ruby on rails - Override foreign key when assigning via the << operator in a has many relationship -
here situation:
class foo has_many :bars end class bar belongs_to :foo end foo1 = foo.create foo2 = foo.create bar1 = foo1.bars.create bar2 = foo1.bars.create as can see, both bars belong foo1, foo_id 1. now:
foo2 << bar2 i set bar2's foo_id 2. seems if foo_id set (since belongs foo1 in case) not overwrite it. reason not want assign bar2's foo nil first not know name of association perspective. actual code looks like:
foo2.bars << bar2 i cannot perform string parsing because model type may not correspond foreign key. disturbed presence of polymorphic associations or sti models. why i'd prefer above "just work" without having arrive at
bar2.foo = nil just because parent model of foo class. clear, let's foo < foobar. if like:
foo2.class.name.foreign_key evaluate "foo_id", when actual foreign key "foo_bar_id".
is there way force overriding foreign key when using << operator?
Comments
Post a Comment