Migrations

I’m trying to understand migrations and here is my problem, lets take
this example code from the API docs for migration. This code creates a
table then adds a new row to it, my problem is how can this add a row
to the table if the model doesn’t exist and how can you create a model
if the table doesn’t first exist. Can the migrations also be told to
create a model for a new table? Because right now if I run this code
it will fail to add the first row because the model does not exist
yet.

class AddSystemSettings < ActiveRecord::Migration
def self.up
create_table :system_settings do |t|
t.column :name, :string
t.column :label, :string
t.column :value, :text
t.column :type, :string
t.column :position, :integer
end

  SystemSetting.create :name => "notice", :label => "Use notice?",

:value => 1
end

def self.down
  drop_table :system_settings
end

end

On Dec 29, 2005, at 7:17 AM, Frank H. wrote:

I’m trying to understand migrations and here is my problem, lets take
this example code from the API docs for migration. This code creates a
table then adds a new row to it, my problem is how can this add a row
to the table if the model doesn’t exist and how can you create a model
if the table doesn’t first exist…

The table does not need to exist in order to define a new model–it
only needs to exist the first time you try to use the model. Thus,
you can use the generator to create a new model, and then reference
the model in the migration that creates the table.

  • Jamis

Jamis B. wrote:

The table does not need to exist in order to define a new model–it
only needs to exist the first time you try to use the model. Thus,
you can use the generator to create a new model, and then reference the
model in the migration that creates the table.

On a related note, is there a clean way to call the scaffold generator
(or any generator, for that matter) from within a migration? The
scaffold generator won’t fly without a table. I know it’s not what the
migrations are supposed to be for, but it’d be damn nice to have :slight_smile:

I was incorrectly assuming you couldn’t generate a model for a table
that did not exist in the DB yet. I was thinking of what Alex was
talking about. Sometimes these things can get confusing (well for at
least me, haha). Thanks for the help.

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