Forum: Ruby on Rails Insert multiple rows in single query wrapped in Rails magic?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
8a3abcfeae74fa0ea68bb23ef0501511?d=identicon&s=25 nathaniel (Guest)
on 2007-04-17 00:07
Hello all,

I'm trying to create a number of records in a single table using only a
single SQL query (to reduce otherwise sizeable communication overhead).

I'm using Postgres, which, like some other databases, allows me to do
this in plain SQL as:

INSERT INTO products (product_no, name, price) VALUES
  (1, ’Cheese’, 9.99),
  (2, ’Bread’, 1.99),
  (3, ’Milk’, 2.99);

Is there a clever trick to wrapping this in Rails without manually
fetching sequence values and constructing my own SQL strings?

I've looked into passing Products.create() an array of hashes, but the
Rails code just iterates over the products, creating each in turn.
Besides, for security, I've disabled the setting of some attributes
through hash-mapping, and it's these attributes I want to initialise in
the records I'm creating.

Thanks, Nat
This topic is locked and can not be replied to.