jorgemanrubia/forceps

Polymorphic STI models don't work

Opened this issue · 4 comments

We're running into the issue where polymorphic STI associations do not work correctly. Here is an example, taken from the tests. I've converted project_id on LineItem to subject_id and subject_type, giving us:

class LineItem < ActiveRecord::Base
  belongs_to :subject, :polymorphic => true
end

class Product < ActiveRecord::Base
  has_many :line_items, :as => :subject
end

However, when I try to load a generated LineItem's subject, I get:

#<RemoteLineItem id: 1, quantity: nil, subject_id: 3, invoice_id: nil, created_at: "2014-08-14 22:48:42", updated_at: "2014-08-14 22:48:42", subject_type: "Cars::German::SportsCar">
#<Forceps::Remote::LineItem id: 1, quantity: nil, subject_id: 3, invoice_id: nil, created_at: "2014-08-14 22:48:42", updated_at: "2014-08-14 22:48:42", subject_type: "Cars::German::SportsCar">

# But if I ask the Forceps::Remote::LineItem about its subject_type, I get Forceps::Remote::Cars::German::SportsCar due to reference_remote_class_in_polymorphic_association.  But if I ask it for subject, I get:

ActiveRecord::RecordNotFound: Couldn't find Forceps::Remote::Cars::German::SportsCar with id=3 [WHERE "products"."type" IN ('Forceps::Remote::Cars::German::SportsCar')]

Which makes sense because the type in the remote db would be Cars::German::SportsCar. Do I need to copy the subject first, then somehow update reference_remote_class_in_polymorphic_association and make_type_attribute_point_to_local_class_if_needed?

Hey @cantino, I am about to take a flight. I'll look at this as soon as I can. In the next days I'll be a little bit disconnected though.

Also, it would be helpful if you can fork and push a failing spec

Yes, I can push up those failing specs early next week.

I think we found a simpler test case in #7.