diff options
author | Eloy Duran <eloy.de.enige@gmail.com> | 2010-01-08 20:47:49 +0100 |
---|---|---|
committer | Eloy Duran <eloy.de.enige@gmail.com> | 2010-01-08 21:45:02 +0100 |
commit | f2aacd51405724cdf7cfd36a439c9dbfce16973a (patch) | |
tree | d12d882bca639002b7a8e4c8b7be537efe7c7d4b /activerecord/lib/active_record/autosave_association.rb | |
parent | 5193fe9dd730f9bbb72db055f37625fe9558b6ca (diff) | |
download | rails-f2aacd51405724cdf7cfd36a439c9dbfce16973a.tar.gz rails-f2aacd51405724cdf7cfd36a439c9dbfce16973a.tar.bz2 rails-f2aacd51405724cdf7cfd36a439c9dbfce16973a.zip |
Rollback the transaction when one of the autosave associations fails to save. [#3391 state:resolved]
Diffstat (limited to 'activerecord/lib/active_record/autosave_association.rb')
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 741fb2ef40..f7119a77cc 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -296,13 +296,15 @@ module ActiveRecord association.destroy(record) elsif autosave != false && (@new_record_before_save || record.new_record?) if autosave - association.send(:insert_record, record, false, false) + saved = association.send(:insert_record, record, false, false) else association.send(:insert_record, record) end elsif autosave - record.save(false) + saved = record.save(false) end + + raise ActiveRecord::Rollback if saved == false end end @@ -329,7 +331,9 @@ module ActiveRecord key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id if autosave != false && (new_record? || association.new_record? || association[reflection.primary_key_name] != key || autosave) association[reflection.primary_key_name] = key - association.save(!autosave) + saved = association.save(!autosave) + raise ActiveRecord::Rollback if !saved && autosave + saved end end end @@ -350,7 +354,7 @@ module ActiveRecord if autosave && association.marked_for_destruction? association.destroy elsif autosave != false - association.save(!autosave) if association.new_record? || autosave + saved = association.save(!autosave) if association.new_record? || autosave if association.updated? association_id = association.send(reflection.options[:primary_key] || :id) @@ -360,6 +364,8 @@ module ActiveRecord self[reflection.options[:foreign_type]] = association.class.base_class.name.to_s end end + + saved if autosave end end end |