has_many_polymorphs icon indicating copy to clipboard operation
has_many_polymorphs copied to clipboard

Double insertions into join model

Open ilyakatz opened this issue 15 years ago • 0 comments

I stumbled upon a situation where join model would get inserted two records whenever has_many_polymorphs was used.

Here is the scenario

class Item < ActiveRecord::Base

after_create :add_info

has_many_polymorphs :describables, :from => [:public_product_infos, :propriatary_product_infos], :through => :product_descriptions

def add_info describables << PublicProductInfo.create(:stuff=>"blah") end

end

I've noticed that this would insert two records into 'product_descriptions' table. Doing backtracing in debug mode it seems that the first insert is triggered by has_many_polymorphs during array append operation (as expected). However, the second one seems to be happening as if it was a regular has_many relationship. The latter one is undesirable. The fix for that was to change

after_create

to

after_save

ilyakatz avatar Oct 16 '10 01:10 ilyakatz