John Mcleod
2009-Oct-26 12:57 UTC
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. 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 -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Oct-26 13:02 UTC
Re: Inserting multiple records from one table (model) to ano
John Mcleod 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. > > JohnMBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
John Mcleod
2009-Oct-26 13:13 UTC
Re: Inserting multiple records from one table (model) to ano
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 Mcleod 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 > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org-- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Oct-26 13:19 UTC
Re: Inserting multiple records from one table (model) to ano
John Mcleod 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
John Mcleod
2009-Oct-26 13:33 UTC
Re: Inserting multiple records from one table (model) to ano
Your right. I could not deal with the additional table. Thanks for the insight. John Marnen Laibow-Koser wrote:> John Mcleod 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 > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org-- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Oct-26 13:40 UTC
Re: Inserting multiple records from one table (model) to ano
John Mcleod 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Ar Chron
2009-Oct-26 14:07 UTC
Re: Inserting multiple records from one table (model) to ano
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. -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Oct-26 14:12 UTC
Re: Inserting multiple records from one table (model) to ano
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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
John Mcleod
2009-Oct-26 14:17 UTC
Re: Inserting multiple records from one table (model) to ano
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.-- Posted via http://www.ruby-forum.com/.
John Mcleod
2009-Oct-27 18:37 UTC
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 -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Oct-27 18:48 UTC
Re: Inserting multiple records from one table (model) to another
John Mcleod 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. > > JohnMYou already asked this at http://www.ruby-forum.com/topic/198008 / 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Jillian Galloway
2009-Oct-29 18:23 UTC
Re: Inserting multiple records from one table (model) to another
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 Mcleod < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
John Mcleod
2009-Oct-29 18:40 UTC
Re: Inserting multiple records from one table (model) to an
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 Galloway 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 Mcleod <-- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Oct-29 19:15 UTC
Re: Inserting multiple records from one table (model) to an
Jillian Galloway 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 Mcleod <Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.