aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2016-09-01 11:20:30 +0900
committerRyuta Kamizono <kamipo@gmail.com>2016-12-28 17:27:21 +0900
commitd4a62096ec5ec53a542f25c17a2d125f93ab21a5 (patch)
treee7fdb51b320ce50cfc4ab938989af16bef3ba73e /activerecord/lib
parentfce3c0cf64f053cf853764e8b47343a814b98d45 (diff)
downloadrails-d4a62096ec5ec53a542f25c17a2d125f93ab21a5.tar.gz
rails-d4a62096ec5ec53a542f25c17a2d125f93ab21a5.tar.bz2
rails-d4a62096ec5ec53a542f25c17a2d125f93ab21a5.zip
Avoid to call `set_inverse_instance` twice for `has_many` association
`create`, `create!`, and `concat` in `has_many` association hits `set_inverse_instance` twice. It is enough to hit only once.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb3
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb1
2 files changed, 2 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index 13f77c7d4d..f0cb108136 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -307,6 +307,8 @@ module ActiveRecord
target << record
end
+ set_inverse_instance(record)
+
yield(record) if block_given?
rescue
if index
@@ -319,7 +321,6 @@ module ActiveRecord
end
callback(:after_add, record) unless skip_callbacks
- set_inverse_instance(record)
record
end
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index 742cd25509..482970cfea 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -38,7 +38,6 @@ module ActiveRecord
def insert_record(record, validate = true, raise = false)
set_owner_attributes(record)
- set_inverse_instance(record)
if raise
record.save!(validate: validate)