You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My concern is how to get a specific collection from has_many in Test::Owner models rather than generic?
When specifying
# adding to super_class.rbbelongs_to:ownerable,polymorphic: true# change in owner.rbhas_many:sub_classes_a,as: :ownerable,class_name: 'Test::SubClassA'
and trying to get specific collection through has_many by invoking owner.sub_classes_a, Rails will automatically include the column test_sub_classes_as.ownerable_id in select query which is only possible in test_super_classes table giving an exception such column not found in table sub_classes_as.
With the following definition
# super_class.rbclassTest::SuperClass < ActiveRecord::Basebelongs_to:ownerable,polymorphic: trueactableas: :superclassableend# super class migrationcreate_table:test_super_classesdo |t|
t.references:ownerable,polymorphic: truet.actableas: :superclassablet.timestampsend# sub_class_a.rbclassTest::SubClassA < ActiveRecord::Baseacts_as:super_class,as: :superclassable,class_name: 'Test::SuperClass'end# sub_class_b.rbclassTest::SubClassB < ActiveRecord::Baseacts_as:super_class,as: :superclassable,class_name: 'Test::SuperClass'end# owner.rb, has_many actablesclassTest::Owner < ActiveRecord::Basehas_many:sub_classes_a,as: :ownerable,class_name: 'Test::SuperClassA'end# special_owner.rb, has_many actables, STI from Test::OwnerclassTest::SpecialOwner < Ownerhas_many:sub_classes_b,as: :ownerable,class_name: 'Test::SuperClassB'end
I am able to do Test::Owner.new(sub_classes_a: [Test::SubClassA.new]).save to save a specific object in the format
// table: test_super_classes (generic)
id: xx
ownerable_id: xx
ownerable_type: Test::Owner
superclassable_id: xx
superclassable_type: Test::SubClassA
...
However, in my design I have to have has_many with specific class_name: 'Test::SubClassA' to enforce only these objects to be stored and referenced from the model. Moreover, SubClassA and SubClassB have to have a reference back to the owner
Thanks!
The text was updated successfully, but these errors were encountered:
Please correct me or suggest,
My concern is how to get a
specific
collection from has_many inTest::Owner
models rather thangeneric
?When specifying
and trying to get specific collection through
has_many
by invokingowner.sub_classes_a
, Rails will automatically include the columntest_sub_classes_as.ownerable_id
in select query which is only possible intest_super_classes
table giving an exceptionsuch column not found in table sub_classes_as
.With the following definition
I am able to do
Test::Owner.new(sub_classes_a: [Test::SubClassA.new]).save
to save a specific object in the formatHowever, in my design I have to have has_many with specific
class_name: 'Test::SubClassA'
to enforce only these objects to be stored and referenced from the model. Moreover,SubClassA
andSubClassB
have to have a reference back to the ownerThanks!
The text was updated successfully, but these errors were encountered: