Forum: Ruby on Rails Question: Migration - multiple creates

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dark Ambient (Guest)
on 2006-06-13 19:07
(Received via mailing list)
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

end

  def self.down
    drop_table :table3s
  end
end
Jim (Guest)
on 2006-06-13 19:16
Maybe you should try it and see?

I can't help you because I honestly don't know, but I do know how I
would proceed.

Dark Ambient wrote:
> I want to create a migration file that creates multiple tables, so i'm
> guessing the correct format would be:
Dark Ambient (Guest)
on 2006-06-13 19:37
(Received via mailing list)
It didn't work.

Stuart
Dark Ambient (Guest)
on 2006-06-13 19:37
(Received via mailing list)
Actually only the first table was created.

Stuart
Jay (Guest)
on 2006-06-13 19:44
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
>
91577d00348e23566f04e26438ace241?d=identicon&s=25 Curtis S. (cuspendlove)
on 2006-06-13 19:50
(Received via mailing list)
On 6/13/06, Dark Ambient <sambient@gmail.com> wrote:
> Actually only the first table was created.

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.  ;)


-Curtis
Dark Ambient (Guest)
on 2006-06-13 20:00
(Received via mailing list)
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:

** Invoke migrate (first_time)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
** Execute migrate

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
Dark Ambient (Guest)
on 2006-06-13 20:03
(Received via mailing list)
Stop the presses ! I got it to work.  Don't ask how :) Format is the
same.

Thank you,
Stuart
Dark Ambient (Guest)
on 2006-06-13 20:23
(Received via mailing list)
Jay,
Wasn't sure if I thanked you for the help, so thank you!

Stuart
Dark Ambient (Guest)
on 2006-06-13 21:08
(Received via mailing list)
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:

:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in
`rake_original_const_missing': uninitialized constant Postlength
(NameError)

Postlength.create = Postlength is the singular name of the table
postlengths
:postlength is the column name in the table.

This is part of the file:

class Addstaticdata < ActiveRecord::Migration
  def self.up
        Postlength.create(:postlength => '7 Days')
        Postlength.create(:postlength => '14 Days')
        Postlength.create(:postlength => '21 Days')
        Postlength.create(:postlength => '30 Days')
        Postlength.create(:postlength => '45 Days')
        Postlength.create(:postlength => '60 Days')
        Edureq.create(:edureq => 'PhD')
        Edureq.create(:edureq => 'Juris Doctorate')
        Edureq.create(:edureq => 'Trade School')
        Contactmethod.create(:contactmethod => 'Direct through details')
        Contactmethod.create(:contactmethod => 'Through website')
        Contactmethod.create(:contactmethod => 'Both direct and
website')
        Securityclear.create(:securityclear => 'None')
        Securityclear.create(:securityclear => 'Active Confidential')
        Securityclear.create(:securityclear => 'Active Top Secret')
        Securityclear.create(:securityclear => 'Active Top Secret SCI')
 end


  def self.down
  end
end

Hope I'm not being a pain here.
Stuart
Jay (Guest)
on 2006-06-13 21:33
Have you created a Postlength model yet?  You'll need that to populate
your table.
Dark Ambient (Guest)
on 2006-06-13 21:47
(Received via mailing list)
Ahh, okay Jay ..thank you.  I was thinking about that possibly being the
cause but left that out of my experiments.
Thank you again, I owe you !

Stuart
This topic is locked and can not be replied to.