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 Buck 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 :-) -- Alex
On Dec 29, 2005, at 7:17 AM, Frank Hale 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
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. On 12/29/05, Alex Young <alex-qV/boFbD8Meu8LGVeLuP/g@public.gmane.org> wrote:> Jamis Buck 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 :-) > > -- > Alex > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >