Skip to content

Polymorphic STI models don't work #6

Description

@cantino

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions