mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Moved #create! method from Validations to Persistence module
This commit is contained in:
parent
3300fdedc7
commit
977a489cfa
2 changed files with 12 additions and 15 deletions
|
@ -36,6 +36,18 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Creates an object just like Base.create but calls <tt>save!</tt> 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
|
# Given an attributes hash, +instantiate+ returns a new instance of
|
||||||
# the appropriate class. Accepts only keys as strings.
|
# the appropriate class. Accepts only keys as strings.
|
||||||
#
|
#
|
||||||
|
|
|
@ -29,21 +29,6 @@ module ActiveRecord
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
include ActiveModel::Validations
|
include ActiveModel::Validations
|
||||||
|
|
||||||
module ClassMethods
|
|
||||||
# Creates an object just like Base.create but calls <tt>save!</tt> 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)
|
|
||||||
yield(object) if block_given?
|
|
||||||
object.save!
|
|
||||||
object
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# The validation process on save can be skipped by passing <tt>validate: false</tt>.
|
# The validation process on save can be skipped by passing <tt>validate: false</tt>.
|
||||||
# The regular Base#save method is replaced with this when the validations
|
# The regular Base#save method is replaced with this when the validations
|
||||||
# module is mixed in, which it is by default.
|
# module is mixed in, which it is by default.
|
||||||
|
|
Loading…
Reference in a new issue