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 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

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs