# Insert multiple rows in single query wrapped in Rails magic?

**URL:** <https://www.ruby-forum.com/t/insert-multiple-rows-in-single-query-wrapped-in-rails-magic/92197>\
**Category:** Rails\
**Created:** [April 17, 2007, 12:07am UTC](https://www.ruby-forum.com/t/insert-multiple-rows-in-single-query-wrapped-in-rails-magic/92197 "2007-04-17T00:07:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 17, 2007, 12:07am UTC](https://www.ruby-forum.com/t/insert-multiple-rows-in-single-query-wrapped-in-rails-magic/92197/1 "2007-04-17T00:07:41Z")

</div>

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
