craig.kaputa-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-17 01:46 UTC
Creating Multiple tables in my Migrations
When I scaffold in a migration for model that I''ve created I''m finding it hard to create more than 1 table within that migration. I think it has something to do with the fact that the ''createservices'' class gets defined within the migration file called in this case 006_create_services. The migration file that gets created looks something like this: class CreateServices < ActiveRecord::Migration def self.up create_table :services do |t| t.column :services_category_id, :integer t.column :account_id, :integer end end def self.down drop_table :services end end Is the proper approach to scaffold in each and every model and load the database 1 table at the time, this seems like a very time consuming method. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You can do more then one at a time. The biggest problem with doing it, is when your migration fails, and you try and migrate back. If it failed halfway, it can be confusing getting it working again. Using the :force=>true on table creation helps. class CreateServices < ActiveRecord::Migration def self.up create_table :services, :force=>true do |t| t.column :services_category_id, :integer t.column :account_id, :integer end create_table :cats, :force=>true do |t| t.column :name, :integer t.column :breed, :integer end add_index :services, :account_id add_index :cats, [:name,:breed] end def self.down drop_table :services drop_table :cats end end craig.kaputa-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> When I scaffold in a migration for model that I''ve created I''m finding > it hard to create more than 1 table within that migration. I think it > has something to do with the fact that the ''createservices'' class gets > defined within the migration file called in this case > 006_create_services. The migration file that gets created looks > something like this: > > class CreateServices < ActiveRecord::Migration > def self.up > create_table :services do |t| > t.column :services_category_id, :integer > t.column :account_id, :integer > end > end > > def self.down > drop_table :services > end > end > > Is the proper approach to scaffold in each and every model and load > the database 1 table at the time, this seems like a very time > consuming method.-- 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 -~----------~----~----~----~------~----~------~--~---
Just one small remark, I would inverse the two drop_table lines: def self.down drop_table :cats drop_table :services end Depending on your RDBMS, it can be needed if you have some db constraints. -- 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 -~----------~----~----~----~------~----~------~--~---