# 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/177898>\
**Category:** Rails\
**Created:** [October 26, 2009, 1:57pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177898 "2009-10-26T13:57:15Z")\
**Posts on this page:** 9\
**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 26, 2009, 1:57pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177898/1 "2009-10-26T13:57:15Z")

</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.  
After doing some preliminary editing to each record I wish to insert all  
records into another table (projects) in one shot.  
I could create a new method in the projects controller and then call the  
“create”, but doesn’t that only insert one record?

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 26, 2009, 2:02pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177898/2 "2009-10-26T14:02:30Z")

</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.  
> After doing some preliminary editing to each record I wish to insert all  
> records into another table (projects) in one shot.  
> I could create a new method in the projects controller and then call the  
> “create”, but doesn’t that only insert one record?

Yes. In this case, if the tables have the same structure, you might  
want to use SQL’s INSERT SELECT syntax (see your DB for details).

But having two tables with identical structure is smelly. Why not just  
use a flag and a named\_scope?

> I need a little clarity on this.
> 
> I know how to delete all records from a table. I wonder if it’s as  
> simple?

Rails doesn’t abstract this AFAIK, in part because this is very rarely  
needed.

> Thank you for any help.
> 
> JohnM

## 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 26, 2009, 2:13pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177898/3 "2009-10-26T14:13:46Z")

</div>

Thanks for the quick reply.

> But having two tables with identical structure is smelly. Why not just  
> use a flag and a named\_scope?  
> I’m not sure what you mean by this.

Yes, both tables have the identical structure but my idea is too delete  
all data from the “import” table after the insert into the “projects”  
table. This way the user has a fresh table to get info from a csv file.

John

Marnen Laibow-Koser wrote:

> 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.  
> > After doing some preliminary editing to each record I wish to insert all  
> > records into another table (projects) in one shot.  
> > I could create a new method in the projects controller and then call the  
> > “create”, but doesn’t that only insert one record?
> 
> Yes. In this case, if the tables have the same structure, you might  
> want to use SQL’s INSERT SELECT syntax (see your DB for details).
> 
> But having two tables with identical structure is smelly. Why not just  
> use a flag and a named\_scope?
> 
> > I need a little clarity on this.
> > 
> > I know how to delete all records from a table. I wonder if it’s as  
> > simple?
> 
> Rails doesn’t abstract this AFAIK, in part because this is very rarely  
> needed.
> 
> > Thank you for any help.
> > 
> > JohnM
> 
> ## 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 26, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177898/4 "2009-10-26T14:19:27Z")

</div>

John M. wrote:

> Thanks for the quick reply.
> 
> > But having two tables with identical structure is smelly. Why not just  
> > use a flag and a named\_scope?  
> > I’m not sure what you mean by this.

Then go look up named\_scope – or, in this case, default\_scope.

> Yes, both tables have the identical structure but my idea is too delete  
> all data from the “import” table after the insert into the “projects”  
> table. This way the user has a fresh table to get info from a csv file.

There’s no reason to have a fresh table. Again, just flag records as  
appropriate.

## 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 26, 2009, 2:33pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177898/5 "2009-10-26T14:33:09Z")

</div>

Your right.  
I could not deal with the additional table.

Thanks for the insight.

John

Marnen Laibow-Koser wrote:

> John M. wrote:
> 
> > Thanks for the quick reply.
> > 
> > > But having two tables with identical structure is smelly. Why not just  
> > > use a flag and a named\_scope?  
> > > I’m not sure what you mean by this.
> 
> Then go look up named\_scope – or, in this case, default\_scope.
> 
> > Yes, both tables have the identical structure but my idea is too delete  
> > all data from the “import” table after the insert into the “projects”  
> > table. This way the user has a fresh table to get info from a csv file.
> 
> There’s no reason to have a fresh table. Again, just flag records as  
> appropriate.
> 
> ## 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 26, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177898/6 "2009-10-26T14:40:16Z")

</div>

John M. wrote:

> Your right.  
> I could not deal with the additional table.

You could – it’s just poor design in most cases.

> Thanks for the insight.

You’re welcome.

> John

## 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 26, 2009, 3:12pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177898/7 "2009-10-26T15:12:26Z")

</div>

Ar Chron wrote:

> Marnen Laibow-Koser wrote:
> 
> > You could – it’s just poor design in most cases.
> 
> Most cases yes, but in some cases a parallel import table is not a bad  
> thing.
> 
> If you put all your data into one table, then all the records going into  
> that table have to pass all the model constraints immediately.

That’s true, of course. But in that case, the tables don’t have the  
same structure, so the smell doesn’t apply.

> From John’s initial post, I read “after doing some preliminary editing”  
> to imply that the records, as imported from CSV, may not satisfy all the  
> constraints of the application.
> 
> It may be in his interest to keep these “dirty” records separate from  
> the “clean” records so he does not have to relax any constraints on the  
> mainstream application data,

You may be right. I hadn’t thought of that from the initial  
description.

> or complicate his existing model  
> constraints by mixing a flag check into the middle of things.

default\_scope is not complicated!

> Ultimately, it’ll be whatever works best for John.

## 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 26, 2009, 3:07pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177898/8 "2009-10-26T15:07:05Z")

</div>

Marnen Laibow-Koser wrote:

> You could – it’s just poor design in most cases.

Most cases yes, but in some cases a parallel import table is not a bad  
thing.

If you put all your data into one table, then all the records going into  
that table have to pass all the model constraints immediately.

From John’s initial post, I read “after doing some preliminary editing”  
to imply that the records, as imported from CSV, may not satisfy all the  
constraints of the application.

It may be in his interest to keep these “dirty” records separate from  
the “clean” records so he does not have to relax any constraints on the  
mainstream application data, or complicate his existing model  
constraints by mixing a flag check into the middle of things.

Ultimately, it’ll be whatever works best for John.

---

<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 26, 2009, 3:17pm UTC](https://www.ruby-forum.com/t/inserting-multiple-records-from-one-table-model-to-another/177898/9 "2009-10-26T15:17:10Z")

</div>

You are correct.  
I must do some editing to incoming data before it’s “imported” into the  
“projects” table.

I’m having more problems importing data from one table to another. When  
editing is complete, I click a button to “save” to projects table.  
My button is very up front and easy.  
\<%= button\_to “Add Records to Projects”, :action =\> “addIrbProjects”,  
:id =\> import.id %\>

It’s just setting up the projects Controller that has me confused.  
I’m in the projects controller reading a “imports” table.

John

Ar Chron wrote:

> Marnen Laibow-Koser wrote:
> 
> > You could – it’s just poor design in most cases.
> 
> Most cases yes, but in some cases a parallel import table is not a bad  
> thing.
> 
> If you put all your data into one table, then all the records going into  
> that table have to pass all the model constraints immediately.
> 
> From John’s initial post, I read “after doing some preliminary editing”  
> to imply that the records, as imported from CSV, may not satisfy all the  
> constraints of the application.
> 
> It may be in his interest to keep these “dirty” records separate from  
> the “clean” records so he does not have to relax any constraints on the  
> mainstream application data, or complicate his existing model  
> constraints by mixing a flag check into the middle of things.
> 
> Ultimately, it’ll be whatever works best for John.
