create
![Wide documentation Importance_3](https://d2vfyqvduarcvs.cloudfront.net/images/importance_3.png?1349367920)
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (0)
- 2.0.3 (11)
- 2.1.0 (11)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-1)
- 3.1.0 (13)
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
create(attributes = nil, options = {}, &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 be a Hash or an Array of Hashes. These Hashes describe the attributes on the objects that are to be created.
create respects mass-assignment security and accepts either :as or :without_protection options in the options parameter.
Examples
# Create a single new object User.create(:first_name => 'Jamie') # Create a single new object using the :admin mass-assignment security role User.create({ :first_name => 'Jamie', :is_admin => true }, :as => :admin) # Create a single new object bypassing mass-assignment security User.create({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true) # 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
![Default_avatar_30](https://www.gravatar.com/avatar/4f5d54213efb6502baab202f3fb2f09e?size=30&default=http://apidock.com/images/default_avatar_30.png)
![Default_avatar_30](https://www.gravatar.com/avatar/3bae08553668f3b35783e6ed6e3ee79b?size=30&default=http://apidock.com/images/default_avatar_30.png)
What's difference between create and new?
What’s difference between create and new?
![Default_avatar_30](https://www.gravatar.com/avatar/5b2b185c814bb25f2f95a1152e58f033?size=30&default=http://apidock.com/images/default_avatar_30.png)
Where this method moved to
After v3.1.0, this method moved to ActiveRecord::Persistence::ClassMethods#create .