module ActiveRecord::Persistence::ClassMethods
def insert_all!(attributes, returning: nil, record_timestamps: nil)
{ id: 1, title: "Eloquent Ruby", author: "Russ" }
{ id: 1, title: "Rework", author: "David" },
Book.insert_all!([
# does not have a unique id.
# Raises ActiveRecord::RecordNotUnique because "Eloquent Ruby"
])
{ title: "Eloquent Ruby", author: "Russ" }
{ title: "Rework", author: "David" },
Book.insert_all!([
# Insert multiple records
==== Examples
record_timestamps: false # Never set timestamps automatically
record_timestamps: true # Always set timestamps automatically
way or the other, pass :record_timestamps:
To override this and force automatic setting of timestamp columns one
behavior.
the model's record_timestamps config, matching typical
By default, automatic setting of timestamp columns is controlled by
[:record_timestamps]
(for example, returning: Arel.sql("id, name as new_name")).
You can also pass an SQL string if you need more control on the return values
clause entirely.
or returning: false to omit the underlying RETURNING SQL
Pass returning: %w[ id name ] for both id and name
inserted records, which by default is the primary key.
(PostgreSQL and SQLite3 only) An array of attributes to return for all successfully
[:returning]
==== Options
:returning (see below).
Returns an ActiveRecord::Result with its contents based on
To skip duplicate rows, see #insert_all. To replace them, see #upsert_all.
unique index on the table. In that case, no rows are inserted.
Raises ActiveRecord::RecordNotUnique if any rows violate a
the attributes for a single row and must have the same keys.
The +attributes+ parameter is an Array of Hashes. Every Hash determines
go through Active Record's type casting and serialization.
Active Record callbacks or validations. Though passed values
statement. It does not instantiate any models nor does it trigger
Inserts multiple records into the database in a single SQL INSERT
def insert_all!(attributes, returning: nil, record_timestamps: nil) InsertAll.new(self, attributes, on_duplicate: :raise, returning: returning, record_timestamps: record_timestamps).execute end