aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/autosave_association.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/autosave_association.rb')
-rw-r--r--activerecord/lib/active_record/autosave_association.rb12
1 files changed, 12 insertions, 0 deletions
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, <tt>self</tt>, and any loaded autosave associations are valid.
def valid_with_autosave_associations?
if valid_without_autosave_associations?