instance method
insert
Ruby on Rails 8.0.4
Since v2.2.3Signature
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
-
arelreq -
nameopt = nil -
pkopt = nil -
id_valueopt = nil -
sequence_nameopt = nil -
bindsopt = [] -
returningkey = 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