From: Joey Date: 2006-11-21T17:41:48+09:00 Subject: Re: Inserting multiple rows from array ------=_Part_101441_1751584.1164098504743 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline contact_ids.map { |num| AssignedJob.create({:job_id => job.id, :contact_id => num}) } On 11/21/06, J. B. Rainsberger wrote: > > David Lelong wrote: > > Hi, > > > > I have two sets of values: > > > > job.id = 1 and contact_ids = [4, 8] > > > > I want to be able to iterate over all of values in the contact_id array > > and create database records consisting of a job_id and contact_id. > > > > So, in this example, the end result would be two new records that > > contain: > > > > -------------------- > > |job_id |contact_id| > > -------------------- > > | 1 | 4 | > > -------------------- > > | 1 | 8 | > > -------------------- > > > > Any advice? > > contact_ids.map { |each| > AssignedJob.new({:job_id => job.id, :contact_id => each}) > }.each { |each| > each.save! > } > > This is ActiveRecord code, or pretty close to it. First, turn the data > into row objects, then ask each one to save itself. > > I hope that helps. > -- > J. B. (Joe) Rainsberger :: http://www.jbrains.ca > Your guide to software craftsmanship > JUnit Recipes: Practical Methods for Programmer Testing > 2005 Gordon Pask Award for contribution Agile Software Practice > > ------=_Part_101441_1751584.1164098504743--