nathaniel
2007-Apr-16 22:07 UTC
Insert multiple rows in single query wrapped in Rails magic?
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---