Skip to content

Double insertions into join model #18

@ilyakatz

Description

@ilyakatz

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

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