From 977a489cfad70dfd0a6a01c4ef792cf6ac0c9046 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Tue, 5 Aug 2014 18:36:29 +0300 Subject: Moved #create! method from Validations to Persistence module --- activerecord/lib/active_record/persistence.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activerecord/lib/active_record/persistence.rb') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 96e44c2f59..f35865f54c 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -36,6 +36,18 @@ module ActiveRecord end end + # Creates an object just like Base.create but calls save! instead of +save+ + # so an exception is raised if the record is invalid. + def create!(attributes = nil, &block) + if attributes.is_a?(Array) + attributes.collect { |attr| create!(attr, &block) } + else + object = new(attributes, &block) + object.save! + object + end + end + # Given an attributes hash, +instantiate+ returns a new instance of # the appropriate class. Accepts only keys as strings. # -- cgit v1.2.3 From 38b6b5db58457b5445d2c42298facf322136c3ae Mon Sep 17 00:00:00 2001 From: Tom Kadwill Date: Wed, 6 Aug 2014 18:52:48 +0100 Subject: [ci skip] Updated create! documentation description and added +attributes+ for rdoc --- activerecord/lib/active_record/persistence.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/persistence.rb') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index f35865f54c..b89e081542 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -36,8 +36,11 @@ module ActiveRecord end end - # Creates an object just like Base.create but calls save! instead of +save+ - # so an exception is raised if the record is invalid. + # Creates an object (or multiple objects) and saves it to the database, if validations pass. + # Works like Base#create except that it raises a RecordInvalid error if validations fail. + # + # The +attributes+ parameter can be either a Hash or an Array of Hashes. + # These Hashes describe the attributes on the objects that are to be created. def create!(attributes = nil, &block) if attributes.is_a?(Array) attributes.collect { |attr| create!(attr, &block) } -- cgit v1.2.3 From 30529dc00f96777c325dff496930f560fb9d59e9 Mon Sep 17 00:00:00 2001 From: Zachary Scott Date: Thu, 7 Aug 2014 19:05:21 -0700 Subject: Rephrase how we explain RecordInvalid exception in the context of `#create!` regarding validations in contrast to the behavior of `#create`. Also describe creating multiple objects using an array of hashes as the +attributes+ parameter. [ci skip] /cc #16384 --- activerecord/lib/active_record/persistence.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/persistence.rb') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index b89e081542..b5799d6bff 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -36,11 +36,13 @@ module ActiveRecord end end - # Creates an object (or multiple objects) and saves it to the database, if validations pass. - # Works like Base#create except that it raises a RecordInvalid error if validations fail. + # Creates an object (or multiple objects) and saves it to the database, + # if validations pass. Raises a RecordInvalid error if validations fail, + # unlike Base#create. # # The +attributes+ parameter can be either a Hash or an Array of Hashes. - # These Hashes describe the attributes on the objects that are to be created. + # These describe which attributes to be created on the object, or + # multiple objects when given an Array of Hashes. def create!(attributes = nil, &block) if attributes.is_a?(Array) attributes.collect { |attr| create!(attr, &block) } -- cgit v1.2.3 From 0f99aa615e11c50cc64b567f54cf64f59108c5e5 Mon Sep 17 00:00:00 2001 From: lsylvester Date: Mon, 11 Aug 2014 13:31:31 +1000 Subject: update error message to reflect that the record could have been destroyed --- activerecord/lib/active_record/persistence.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/persistence.rb') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index b5799d6bff..51b1931ed5 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -287,7 +287,8 @@ module ActiveRecord # This method raises an +ActiveRecord::ActiveRecordError+ when called on new # objects, or when at least one of the attributes is marked as readonly. def update_columns(attributes) - raise ActiveRecordError, "cannot update on a new record object" unless persisted? + raise ActiveRecordError, "cannot update a new record" if new_record? + raise ActiveRecordError, "cannot update a destroyed record" if destroyed? attributes.each_key do |key| verify_readonly_attribute(key.to_s) -- cgit v1.2.3