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 be a Hash or an Array of Hashes. These Hashes describe the attributes on the objects that are to be created.


  # 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

  # 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
Show source
Register or log in to add new notes.
December 13, 2009
1 thank


create generates the object and saves. new only generates the object.


o = Object.new(:foo => 'bar')

is the same as

o = Object.create(:foo => 'bar')
December 13, 2009
0 thanks

What's difference between create and new?

What’s difference between create and new?

May 25, 2016 - (v1.0.0 - v3.1.0)
0 thanks