method
create
v4.1.8 -
Show latest stable
-
2 notes -
Class: ClassMethods
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (-38)
- 4.1.8 (-15)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
create(attributes = nil, &block)
public
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
Register or
log in
to add new notes.
roryokane -
May 25, 2016
0 thanks
Location of this method in older versions
Before v3.2.1, this method was in ActiveRecord::Base.create .
nidhinnambiar -
May 25, 2016
0 thanks
Active Record Import
With activerecord-import, you’d just add your records to an array and call import:
records_to_import = pricing_data.map do |location, price| Inventory.new(location: location, price: price) end Inventory.import records_to_import