diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-06-14 09:21:31 -0600 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-06-17 10:27:02 -0600 |
commit | 068f092ced8483e557725542dd919ab7c516e567 (patch) | |
tree | 26a8fb8e3776fef2a6133b537060742605c09008 /activerecord/lib | |
parent | e7a8fda0336fe733adbdd0bdb642a09327ef22f1 (diff) | |
download | rails-068f092ced8483e557725542dd919ab7c516e567.tar.gz rails-068f092ced8483e557725542dd919ab7c516e567.tar.bz2 rails-068f092ced8483e557725542dd919ab7c516e567.zip |
Don't save through records twice
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.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 5 |
1 files changed, 2 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index b3c3e26c9f..dd92e29199 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -184,9 +184,7 @@ module ActiveRecord before_save :before_save_collection_association define_non_cyclic_method(save_method) { save_collection_association(reflection) } - # Doesn't use after_save as that would save associations added in after_create/after_update twice - after_create save_method - after_update save_method + after_save save_method elsif reflection.has_one? define_method(save_method) { save_has_one_association(reflection) } unless method_defined?(save_method) # Configures two callbacks instead of a single after_save so that @@ -364,6 +362,7 @@ module ActiveRecord raise ActiveRecord::Rollback unless saved end + @new_record_before_save = false end # reconstruct the scope now that we know the owner's id |