aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorAlan Kennedy <alankennedy100@gmail.com>2012-11-25 21:01:27 +0000
committerAlan Kennedy <alankennedy100@gmail.com>2013-10-31 18:16:29 +0000
commitf74a5616e89f03e13a025401475ac1101bb159ae (patch)
treedf6a26ff670af4d3e35e4efd350ab203644cc803 /activerecord/lib
parent6e5d409f659c325871cac4cca79526e815644288 (diff)
downloadrails-f74a5616e89f03e13a025401475ac1101bb159ae.tar.gz
rails-f74a5616e89f03e13a025401475ac1101bb159ae.tar.bz2
rails-f74a5616e89f03e13a025401475ac1101bb159ae.zip
Save has_one associations only if record has changes
Prevents save related callbacks such as `after_commit` being triggered when `has_one` objects are already persisted and have no changes.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/autosave_association.rb5
1 files changed, 3 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index e9622ca0c1..e9e35f4e21 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -377,14 +377,15 @@ module ActiveRecord
def save_has_one_association(reflection)
association = association_instance_get(reflection.name)
record = association && association.load_target
+
if record && !record.destroyed?
autosave = reflection.options[:autosave]
if autosave && record.marked_for_destruction?
record.destroy
- else
+ elsif autosave != false
key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id
- if autosave != false && (autosave || new_record? || record_changed?(reflection, record, key))
+ if (autosave && record.changed_for_autosave?) || new_record? || record_changed?(reflection, record, key)
unless reflection.through_reflection
record[reflection.foreign_key] = key