I have added a new column default to a table named property 开发者_如何学Pythonin my database through a migration.
class AddDefaultToProperty < ActiveRecord::Migration
def self.up
add_column :property, :is_default, :boolean
end
def self.down
remove_column :property, :is_default
end
end
The default column contains a boolean value which says if the property is a default one or not. Now I need a way to populate that column for some specific rows. Which is the best way to do this task? The default properties will probably change in the near future so I need some flexible way to contemplate this situation.
USING RAILS 2.3.10
Thanks!
That depends. If it's an integral part of the migration and you want that data to be available immediately after the migration completes, then you should put that script directly into your migration. If the data availability can wait, you can also opt for creating a rake task to populate the data - the upside is that your migration will be faster and that the rake task is optional - the downside is that you have to run it manually.
I would recoment changing the data in the migration. This ensures that further migrations will always work as expected.
However, always ensure you create your own models for use within migrations, otherwise conflicats between newer models and older schemas will get you.
Just add this line the top of your migration:
class Property < ActiveRecord::Base; end
精彩评论