Inserting multiple records from one table (model) to another

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

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://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
[email protected]

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. :slight_smile:

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

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

    @imports.each { |import|
    if @projects.create!(params)
    flash[‘notice’] = ‘CSV projects have been successfully created
    in Projects Database.’
    redirect_to :controller => ‘projects’, :action => ‘index’
    end
    }
    end

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. :slight_smile:

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

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. :slight_smile:

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
[email protected]