aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-12-12 11:19:32 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2013-12-12 11:19:32 -0800
commitc76549d6de3716782d227c370a2169450274ed97 (patch)
treeef980772ba53feba22b3fc9f7357f78baf3ade70
parentda3891c898fd79ab28dad3ce4c9e52d876c9e4e9 (diff)
downloadrails-c76549d6de3716782d227c370a2169450274ed97.tar.gz
rails-c76549d6de3716782d227c370a2169450274ed97.tar.bz2
rails-c76549d6de3716782d227c370a2169450274ed97.zip
remove the nil check from set_inverse_instance
methods that call set_inverse_instance with a record will not have to pay the cost of a nil check on every call
-rw-r--r--activerecord/lib/active_record/associations/association.rb3
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_association.rb2
-rw-r--r--activerecord/lib/active_record/associations/preloader/singular_association.rb2
-rw-r--r--activerecord/lib/active_record/associations/singular_association.rb4
4 files changed, 7 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb
index ce7d41cf54..67ea489b22 100644
--- a/activerecord/lib/active_record/associations/association.rb
+++ b/activerecord/lib/active_record/associations/association.rb
@@ -104,11 +104,12 @@ module ActiveRecord
# Set the inverse association, if possible
def set_inverse_instance(record)
- if record && invertible_for?(record)
+ if invertible_for?(record)
inverse = record.association(inverse_reflection_for(record).name)
inverse.target = owner
inverse.inversed = true
end
+ record
end
# Returns the class of the target. belongs_to polymorphic overrides this to look at the
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb
index e1fa5225b5..5ddff13711 100644
--- a/activerecord/lib/active_record/associations/belongs_to_association.rb
+++ b/activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -12,7 +12,7 @@ module ActiveRecord
update_counters(record)
replace_keys(record)
- set_inverse_instance(record)
+ set_inverse_instance(record) if record
@updated = true if record
diff --git a/activerecord/lib/active_record/associations/preloader/singular_association.rb b/activerecord/lib/active_record/associations/preloader/singular_association.rb
index 2b5cfda8ce..f60647a81e 100644
--- a/activerecord/lib/active_record/associations/preloader/singular_association.rb
+++ b/activerecord/lib/active_record/associations/preloader/singular_association.rb
@@ -11,7 +11,7 @@ module ActiveRecord
association = owner.association(reflection.name)
association.target = record
- association.set_inverse_instance(record)
+ association.set_inverse_instance(record) if record
end
end
diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb
index 02dc464536..e4500af5b2 100644
--- a/activerecord/lib/active_record/associations/singular_association.rb
+++ b/activerecord/lib/active_record/associations/singular_association.rb
@@ -39,7 +39,9 @@ module ActiveRecord
end
def find_target
- scope.first.tap { |record| set_inverse_instance(record) }
+ if record = scope.first
+ set_inverse_instance record
+ end
end
def replace(record)