Module: ActiveRecord::Persistence::ClassMethods
- Defined in:
- lib/active_record/persistence.rb
Instance Method Summary collapse
-
#create(attributes = nil, &block) ⇒ Object
Creates an object (or multiple objects) and saves it to the database, if validations pass.
-
#create!(attributes = nil, &block) ⇒ Object
Creates an object (or multiple objects) and saves it to the database, if validations pass.
-
#instantiate(attributes, column_types = {}) ⇒ Object
Given an attributes hash,
instantiate
returns a new instance of the appropriate class.
Instance Method Details
#create(attributes = nil, &block) ⇒ Object
Creates an object (or multiple objects) and saves it to the database, if validations pass. The resulting object is returned whether the object was saved successfully to the database or not.
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.
Examples
# Create a single new object
User.create(first_name: 'Jamie')
# Create an Array of new objects
User.create([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }])
# Create a single object and pass it into a block to set other attributes.
User.create(first_name: 'Jamie') do |u|
u.is_admin = false
end
# Creating an Array of new objects using a block, where the block is executed for each object:
User.create([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }]) do |u|
u.is_admin = false
end
29 30 31 32 33 34 35 36 37 |
# File 'lib/active_record/persistence.rb', line 29 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 |
#create!(attributes = nil, &block) ⇒ Object
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 describe which attributes to be created on the object, or multiple objects when given an Array of Hashes.
46 47 48 49 50 51 52 53 54 |
# File 'lib/active_record/persistence.rb', line 46 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 |
#instantiate(attributes, column_types = {}) ⇒ Object
Given an attributes hash, instantiate
returns a new instance of the appropriate class. Accepts only keys as strings.
For example, Post.all
may return Comments, Messages, and Emails by storing the record’s subclass in a type
attribute. By calling instantiate
instead of new
, finder methods ensure they get new instances of the appropriate class for each record.
See ActiveRecord::Inheritance#discriminate_class_for_record to see how this “single-table” inheritance mapping is implemented.
66 67 68 69 70 |
# File 'lib/active_record/persistence.rb', line 66 def instantiate(attributes, column_types = {}) klass = discriminate_class_for_record(attributes) attributes = klass.attributes_builder.build_from_database(attributes, column_types) klass.allocate.init_with('attributes' => attributes, 'new_record' => false) end |