From db5d6950169f8f10b6aec85faa2c38e0c57315c7 Mon Sep 17 00:00:00 2001 From: Eloy Duran Date: Wed, 4 Feb 2009 21:40:53 +0100 Subject: Also save :autosave enabled associations when #save! is used. Signed-off-by: Michael Koziarski [#1877 state:committed] --- activerecord/lib/active_record/autosave_association.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 07660ebd03..680b41518c 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -129,6 +129,7 @@ module ActiveRecord base.class_eval do alias_method_chain :reload, :autosave_associations alias_method_chain :save, :autosave_associations + alias_method_chain :save!, :autosave_associations alias_method_chain :valid?, :autosave_associations %w{ has_one belongs_to has_many has_and_belongs_to_many }.each do |type| @@ -161,6 +162,17 @@ module ActiveRecord end end + # Attempts to save the record just like save_with_autosave_associations but + # will raise a RecordInvalid exception instead of returning false if the + # record is not valid. + def save_with_autosave_associations! + if valid_with_autosave_associations? + save_with_autosave_associations(false) || raise(RecordNotSaved) + else + raise RecordInvalid.new(self) + end + end + # Returns whether or not the parent, self, and any loaded autosave associations are valid. def valid_with_autosave_associations? if valid_without_autosave_associations? -- cgit v1.2.3