evan / has_many_polymorphs

An ActiveRecord plugin for self-referential and double-sided polymorphic associations.
http://blog.evanweaver.com/files/doc/fauna/has_many_polymorphs/
Academic Free License v3.0
201 stars 57 forks source link

Double insertions into join model #18

Open ilyakatz opened 14 years ago

ilyakatz commented 14 years ago

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