I want to create a migration file that creates multiple tables, so i’m
guessing the correct format would be:
Can anyone confirm ? I’m guessing that each new create script requires a
seperate class so formatted this way. Perhaps I’m wrong.
TIA
Stuart
class CreateTable1 < ActiveRecord::Migration
def self.up
create_table :table1s do |t|
t.column :length, :string
end
end
def self.down
drop_table :table1s
end
end
class CreateTable2 < ActiveRecord::Migration
def self.up
create_table :table2s do |t|
t.column :length, :string
end
end
def self.down
drop_table :table2s
end
end
class CreateTable3 < ActiveRecord::Migration
def self.up
create_table :table3s do |t|
t.column :length, :string
end
You can put multiple table definition blocks into a single class
declaration. When the migration is called, it only calls the
migration class that corresponds to the migration file name…
Remember, convention over configuration.
You can only have one set of self.up/self.down methods in a migration
class, and only one class per file, but you can create, destroy, or
modify multiple tables in each up and down. So, you’d want something
like:
class CreateTables < ActiveRecord::Migration
def self.up
create_table :table1s do |t|
t.column :length, :string
end
create_table :table2s do |t|
t.column :length, :string
end
create_table :table3s do |t|
t.column :length, :string
end
end
def self.down
drop_table :table1s
drop_table :table2s
drop_table :table3s
end
end
I ran into a migration snag again. I’ve googled around and come up with
a
few things but none that specifically show my scenario. I’m tryint to
migrate data into multiple tables at once.
Tried a few different ways with no success -
I get this error:
Jay, thanks actually right before your email came through that is what I
had
come up with:
Only it’s not working, not coughing errors but not working. I usually
run
through the IDE RadRails, and received just a
(in C:/InstantRails/rails_apps/lfw)
So I went into the rails console (script/console) and I guess that might
have not been a good idea. I ran rake migrate and got back:
Yet, this was not the first time I ran rake migrate. So I tried it
again in
the cosole but added --trace and got back:
(in C:/InstantRails/rails_apps/lfw)
No tables were created though.
Here is the migration file I created.
class CreateTables < ActiveRecord::Migration
def self.up
create_table :postlengths do |t|
t.column :postlength, :string
end
create_table :expreqs do |t|
t.column :expreq, :string
end
create_table :edureqs do |t|
t.column :edureq, :string
end
create_table :contactmethods do |t|
t.column :contactmethod, :string
end
create_table :securityclears do |t|
t.column :securityclear, :string
end
end
def self.down
drop_table :postlengths
drop_table :expreqs
drop_table :edureqs
drop_table :contactmethods
drop_table :securityclears
end
end
Not sure what’s going on.
Stuart
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.