Skip to content
Browse files

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
  • Loading branch information...
1 parent da3891c commit c76549d6de3716782d227c370a2169450274ed97 @tenderlove tenderlove committed Dec 12, 2013
View
3 activerecord/lib/active_record/associations/association.rb
@@ -104,11 +104,12 @@ def reset_scope
# 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
View
2 activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -12,7 +12,7 @@ def replace(record)
update_counters(record)
replace_keys(record)
- set_inverse_instance(record)
+ set_inverse_instance(record) if record
@updated = true if record
View
2 activerecord/lib/active_record/associations/preloader/singular_association.rb
@@ -11,7 +11,7 @@ def preload(preloader)
association = owner.association(reflection.name)
association.target = record
- association.set_inverse_instance(record)
+ association.set_inverse_instance(record) if record
end
end
View
4 activerecord/lib/active_record/associations/singular_association.rb
@@ -39,7 +39,9 @@ def create_scope
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)

0 comments on commit c76549d

Please sign in to comment.
Something went wrong with that request. Please try again.