Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix collection association #create method #18155

Merged

Conversation

bogdan
Copy link
Contributor

@bogdan bogdan commented Dec 23, 2014

When same association is loaded in the model creation callback
The new object is inserted into association twice.

Car.has_many :bulbs
Bulb.belongs_to :car
Bulb.after_create do |record|
  record.car.bulbs.to_a # force loading
end

car = Car.create!
car.bulbs << Bulb.new
assert_equal 1, car.bulbs.size # returns 2 right now

@sgrif
Copy link
Contributor

sgrif commented Dec 23, 2014

Almost this exact change was introduced a few months back, but was reverted because it introduced another bug. I'll see if I can find it.

@magikid
Copy link

magikid commented Aug 18, 2015

@sgrif did you ever find that other big?

@bogdan
Copy link
Contributor Author

bogdan commented Nov 18, 2015

@sgrif do you have some time to look into it again?

@yui-knk
Copy link
Contributor

yui-knk commented Nov 18, 2015

It looks like this PR conflicts with master branch :)

@sgrif
Copy link
Contributor

sgrif commented Nov 18, 2015

I'll look into it

When same association is loaded in the model creation callback
The new object is inserted into association twice
@bogdan bogdan force-pushed the collection_association_double_element_fix branch from 982f341 to 6d0d83a Compare November 23, 2015 14:22
@bogdan
Copy link
Contributor Author

bogdan commented Nov 23, 2015

rebased

@sgrif
Copy link
Contributor

sgrif commented Nov 29, 2015

Welp, can't find what I was thinking of. If I actually did revert a change for causing problems, I expect that I would have added a test case. So ¯_(ツ)_/¯

sgrif added a commit that referenced this pull request Nov 29, 2015
…lement_fix

Bugfix collection association #create method
@sgrif sgrif merged commit 3f16a09 into rails:master Nov 29, 2015
vipulnsward added a commit to vipulnsward/rails that referenced this pull request Feb 16, 2016
senny added a commit that referenced this pull request Feb 16, 2016
Add missing CHANGELOG for regression fix in #18155 which fixes #13387
kamipo added a commit to kamipo/rails that referenced this pull request Apr 20, 2017
… parent association saved in the callback

Related rails#18155, rails#26661, 268a5bb, rails#27434, rails#27442, and rails#28599.

Originally rails#18155 was introduced for preventing double insertion caused
by the after save callback. But it was caused the before save issue
(rails#26661). 268a5bb fixed rails#26661, but it was caused the performance
regression (rails#27434). rails#27442 added new record to `target` before calling
callbacks for fixing rails#27434. But it was caused double firing before save
callback (rails#28599). We cannot add new object to `target` before saving
the object.

This is improving rails#18155 to only track callbacks after `save`.

Fixes rails#28599.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants