diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-12-12 11:29:02 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-12-12 11:29:02 -0800 |
commit | 1678e959e973de32287b65c52ebc6cce87148951 (patch) | |
tree | 6e80f9b31e75902a5d51e77cb79ff7a48a8c55cb /activerecord/lib | |
parent | c76549d6de3716782d227c370a2169450274ed97 (diff) | |
download | rails-1678e959e973de32287b65c52ebc6cce87148951.tar.gz rails-1678e959e973de32287b65c52ebc6cce87148951.tar.bz2 rails-1678e959e973de32287b65c52ebc6cce87148951.zip |
remove nil check
calling replace_keys could possibly do a nil check twice, this commit
reduces it to once.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/belongs_to_association.rb | 16 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb | 2 |
2 files changed, 11 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 5ddff13711..7d96f0c372 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -11,7 +11,11 @@ module ActiveRecord raise_on_type_mismatch!(record) if record update_counters(record) - replace_keys(record) + if record + replace_keys(record) + else + remove_keys + end set_inverse_instance(record) if record @updated = true if record @@ -58,11 +62,11 @@ module ActiveRecord end def replace_keys(record) - if record - owner[reflection.foreign_key] = record[reflection.association_primary_key(record.class)] - else - owner[reflection.foreign_key] = nil - end + owner[reflection.foreign_key] = record[reflection.association_primary_key(record.class)] + end + + def remove_keys + owner[reflection.foreign_key] = nil end def foreign_key_present? diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb index eae5eed3a1..81d4abfa68 100644 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -11,7 +11,7 @@ module ActiveRecord def replace_keys(record) super - owner[reflection.foreign_type] = record && record.class.base_class.name + owner[reflection.foreign_type] = record.class.base_class.name end def different_target?(record) |