Forum: Ruby on Rails migrations

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.
Frank H. (Guest)
on 2005-12-29 16:18
(Received via mailing list)
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
Jamis B. (Guest)
on 2005-12-29 16:39
(Received via mailing list)
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
Alex Y. (Guest)
on 2005-12-29 16:42
(Received via mailing list)
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 :-)
Frank H. (Guest)
on 2005-12-29 16:48
(Received via mailing list)
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 topic is locked and can not be replied to.