Method: Sequel::Dataset#insert
- Defined in:
- lib/sequel/dataset/actions.rb
#insert(*values) ⇒ Object
Inserts values into the associated table. The returned value is generally the value of the primary key for the inserted row, but that is adapter dependent.
insert handles a number of different argument formats:
- no arguments or single empty hash
-
Uses DEFAULT VALUES
- single hash
-
Most common format, treats keys as columns an values as values
- single array
-
Treats entries as values, with no columns
- two arrays
-
Treats first array as columns, second array as values
- single Dataset
-
Treats as an insert based on a selection from the dataset given, with no columns
- array and dataset
-
Treats as an insert based on a selection from the dataset given, with the columns given by the array.
Examples:
DB[:items].insert
# INSERT INTO items DEFAULT VALUES
DB[:items].insert({})
# INSERT INTO items DEFAULT VALUES
DB[:items].insert([1,2,3])
# INSERT INTO items VALUES (1, 2, 3)
DB[:items].insert([:a, :b], [1,2])
# INSERT INTO items (a, b) VALUES (1, 2)
DB[:items].insert(:a => 1, :b => 2)
# INSERT INTO items (a, b) VALUES (1, 2)
DB[:items].insert(DB[:old_items])
# INSERT INTO items SELECT * FROM old_items
DB[:items].insert([:a, :b], DB[:old_items])
# INSERT INTO items (a, b) SELECT * FROM old_items
282 283 284 |
# File 'lib/sequel/dataset/actions.rb', line 282 def insert(*values) execute_insert(insert_sql(*values)) end |