flyer88
flyer88

Reputation: 1093

Populating a newly added column in Rails

I have added a new column default to a table named property in 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!

Upvotes: 0

Views: 1098

Answers (2)

thomasfedb
thomasfedb

Reputation: 5983

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

Upvotes: 0

Pan Thomakos
Pan Thomakos

Reputation: 34350

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.

Upvotes: 2

Related Questions