static public Method

Base.create(attributes = nil)

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.

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'}])

Source Code

# File active_record/base.rb, line 567
def create(attributes = nil)
  if attributes.is_a?(Array)
    attributes.collect { |attr| create(attr) }
  else
    object = new(attributes)
    object.save
    object
  end
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.