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

Don't save through associations twice (4-0-stable) #15731

Merged
merged 1 commit into from Jun 19, 2014

Conversation

sgrif
Copy link
Contributor

@sgrif sgrif commented Jun 14, 2014

If the through record gets created in an after_create hook that is
defined before the association is defined (therefore after its
after_create hook) get saved twice. This ensures that the through
records are created only once, regardless of the order of the hooks.

Backports the fix for #3798 to 4.1. Slightly more conservative on the
callback order since it's a patch version, and required an additional
special case for HABTM

If the through record gets created in an after_create hook that is
defined before the association is defined (therefore after its
after_create hook) get saved twice. This ensures that the through
records are created only once, regardless of the order of the hooks.

Backports the fix for rails#3798 to 4.1. Slightly more conservative on the
callback order since it's a patch version, and required an additional
special case for HABTM
@@ -359,6 +359,7 @@ def save_collection_association(reflection)

raise ActiveRecord::Rollback unless saved
end
@new_record_before_save = false unless reflection.macro == :has_and_belongs_to_many
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we're trying to reduce the use of checking symbols like this throughout the code base, as well as removing the :has_man_belongs_to_many symbol overall. I'm not sure what a better way to do this would be but I don't think this is the best way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is against 4-0-stable, not master.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh whoops carry on then 😊

On Jun 14, 2014, at 12:06 PM, Sean Griffin notifications@github.com wrote:

In activerecord/lib/active_record/autosave_association.rb:

@@ -359,6 +359,7 @@ def save_collection_association(reflection)

         raise ActiveRecord::Rollback unless saved
       end
  •      @new_record_before_save = false unless reflection.macro == :has_and_belongs_to_many
    
    This is against 4-0-stable, not master.


Reply to this email directly or view it on GitHub.

@rafaelfranca rafaelfranca merged commit 69eaac9 into rails:4-0-stable Jun 19, 2014
rafaelfranca added a commit that referenced this pull request Jun 19, 2014
Don't save through associations twice (4-0-stable)

Conflicts:
	activerecord/test/cases/associations/has_many_through_associations_test.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants