instance method insert

Ruby on Rails 8.0.4

Since v2.2.3

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [], returning: nil)

Executes an INSERT query and returns the new record’s ID

id_value will be returned unless the value is nil, in which case the database will attempt to calculate the last inserted id and return that value.

If the next id was calculated in advance (as in Oracle), it should be passed in as id_value. Some adapters support the returning keyword argument which allows defining the return value of the method: nil is the default value and maintains default behavior. If an array of column names is passed - an array of is returned from the method representing values of the specified columns from the inserted row.

Parameters

arel req
name opt = nil
pk opt = nil
id_value opt = nil
sequence_name opt = nil
binds opt = []
returning key = nil
Source
# File activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb, line 195
      def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [], returning: nil)
        sql, binds = to_sql_and_binds(arel, binds)
        value = exec_insert(sql, name, binds, pk, sequence_name, returning: returning)

        return returning_column_values(value) unless returning.nil?

        id_value || last_inserted_id(value)
      end

Defined in activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb line 195 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::ConnectionAdapters::DatabaseStatements

Type at least 2 characters to search.

↑↓ navigate · open · esc close