v6.1.3.1 - Show latest stable - 0 notes - Class: ClassMethods
insert_all!(attributes, returning: nil) public

Inserts multiple records into the database in a single SQL INSERT statement. It does not instantiate any models nor does it trigger Active Record callbacks or validations. Though passed values go through Active Record’s type casting and serialization.

The attributes parameter is an Array of Hashes. Every Hash determines the attributes for a single row and must have the same keys.

Raises ActiveRecord::RecordNotUnique if any rows violate a unique index on the table. In that case, no rows are inserted.

To skip duplicate rows, see ActiveRecord::Persistence#insert_all. To replace them, see ActiveRecord::Persistence#upsert_all.

Returns an ActiveRecord::Result with its contents based on :returning (see below).



(PostgreSQL only) An array of attributes to return for all successfully inserted records, which by default is the primary key. Pass returning: %w[ id name ] for both id and name or returning: false to omit the underlying RETURNING SQL clause entirely.


# Insert multiple records
  { title: "Rework", author: "David" },
  { title: "Eloquent Ruby", author: "Russ" }

# Raises ActiveRecord::RecordNotUnique because "Eloquent Ruby"
# does not have a unique id.
  { id: 1, title: "Rework", author: "David" },
  { id: 1, title: "Eloquent Ruby", author: "Russ" }
Show source
Register or log in to add new notes.