Hi all, I was wondering how to create a rails migration which, from one preexisting and populated table, creates two new tables, each populated with a subset of the columns of the old, big table. I suppose it is possible and easy, but I haven´t found any example out there. Thanks, Alvaro. -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Glaesemann
2007-Oct-08 15:45 UTC
Re: Populating from existing tables with migration
On Oct 8, 2007, at 10:39 , Alvaro Perez wrote:> I was wondering how to create a rails migration which, from one > preexisting and populated table, creates two new tables, each > populated > with a subset of the columns of the old, big table.You can do this by executing raw SQL in the migration (via the execute method). I don''t know if you can do it using the migration DSL. Michael Glaesemann grzm seespotcode net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/8/07, Alvaro Perez <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Hi all, > > I was wondering how to create a rails migration which, from one > preexisting and populated table, creates two new tables, each populated > with a subset of the columns of the old, big table. I suppose it is > possible and easy, but I haven´t found any example out there. >Rails doesn''t have a "create table as select" construct like some databases have. You can use ActiveRecord::Base.execute to run any arbitrary SQL you wish. However, this makes your migration very database-specific. If you want to stick with the migration DSL, you need to create the new tables, then load the data (you can use any of the AR methods like find, create, etc.), and finally drop the old table. If you''re going to use AR methods, you should define "empty" model classes for each of the three tables inside your migration class, so your migration doesn''t trip over validations, associations, etc. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 10/8/07, Bob Showalter <showaltb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:>tabase-specific.> > If you want to stick with the migration DSL, you need to create the > new tables, then load the data (you can use any of the AR methods like > find, create, etc.), and finally drop the old table. > > If you''re going to use AR methods, you should define "empty" model > classes for each of the three tables inside your migration class, so > your migration doesn''t trip over validations, associations, etc.This also ensures that old migrations won''t break as the REAL models code evolves. The models don''t even need to be ''empty'' I''ve found it useful to add methods to the models ''re''defined in a migration for some manipulations. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---