# Inserting multiple records from one table (model) to another

**URL:** <https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177999>\
**Category:** Rails\
**Created:** [October 27, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177999 "2009-10-27T19:37:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [October 27, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177999/1 "2009-10-27T19:37:40Z")

</div>

Hello all,  
I’ve been getter better with Rails but I’m still just learning.

Here’s what I have.  
I have one table (imports) and I read in a csv into this table.

I must do some preliminary editing to each record, afterwhich, I wish to  
insert all records into another table (projects) in one shot.

I’ve created a button below the table

\<%= button\_to “Add Records to Projects”, :action =\> ‘add\_new\_projects’  
%\>

I need a little clarity on this.

I know how to delete all records from a table. I wonder if it’s as  
simple?

Thank you for any help.

JohnM

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [October 27, 2009, 7:48pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177999/2 "2009-10-27T19:48:22Z")

</div>

John M. wrote:

> Hello all,  
> I’ve been getter better with Rails but I’m still just learning.
> 
> Here’s what I have.  
> I have one table (imports) and I read in a csv into this table.
> 
> I must do some preliminary editing to each record, afterwhich, I wish to  
> insert all records into another table (projects) in one shot.
> 
> I’ve created a button below the table
> 
> \<%= button\_to “Add Records to Projects”, :action =\> ‘add\_new\_projects’  
> %\>
> 
> I need a little clarity on this.
> 
> I know how to delete all records from a table. I wonder if it’s as  
> simple?
> 
> Thank you for any help.
> 
> JohnM

You already asked this at [Inserting multiple records from one table (model) to another - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/198008) /  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/7f09e32544d1b4f1/ede1159a796d9e64](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/7f09e32544d1b4f1/ede1159a796d9e64)  
, and got some answers. Let’s keep further discussion to that thread.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [October 29, 2009, 7:26pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177999/3 "2009-10-29T19:26:04Z")

</div>

I think you have to loop through the list saving one record each time,  
but I  
could be wrong. If anyone’s got a better way I’d love to know what it  
is. 🙂

On Tue, Oct 27, 2009 at 2:37 PM, John M. \<

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [October 29, 2009, 7:40pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177999/4 "2009-10-29T19:40:45Z")

</div>

Thank you for the reply.  
I’ve been struggling with this for some time.

Recently, I have this…

- controller -  
def create\_csv\_projects  
@imports = Import.all  
@projects = Project.all

Unfortunately, it not working.

John

Jillian G. wrote:

> I think you have to loop through the list saving one record each time,  
> but I  
> could be wrong. If anyone’s got a better way I’d love to know what it  
> is. 🙂
> 
> On Tue, Oct 27, 2009 at 2:37 PM, John M. \<

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [October 29, 2009, 8:15pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177999/5 "2009-10-29T20:15:55Z")

</div>

Jillian G. wrote:

> I think you have to loop through the list saving one record each time,  
> but I  
> could be wrong.

And so you are. It is never appropriate to put an SQL query inside a  
loop.

> If anyone’s got a better way I’d love to know what it  
> is. 🙂

Use something like ar-extensions if possible. If not, then build an SQL  
query string directly.

> On Tue, Oct 27, 2009 at 2:37 PM, John M. \<

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
