8

For example i have this model:

class Product < ActiveRecord::Base
  attr_accessible :name, :order
end

Then when i did rake db:migrate it created this db/migrate/20120825132038_create_products.rb:

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.integer :order
      t.string :name

      t.timestamps
    end
  end
end

But it all happend cuz i used rails generate Product order:integer name:string

Now after i go to Product model and changes it manually to:

class Product < ActiveRecord::Base
  attr_accessible :name, :order, :category_id

  validates :name, uniqueness: true
  belongs_to :category
end

How can i auto update the db/migrate/20120825132038_create_products.rb with the updates?

Pritesh Jain
  • 9,106
  • 4
  • 37
  • 51
Danpe
  • 18,668
  • 21
  • 96
  • 131

2 Answers2

17

When you ran rake db:migrate, it did not create db/migrate/20120825132038_create_products.rb. That migration file was created when you ran

rails generate Product order:integer name:string

attr_accessible has nothing to do with migrating your database.

I strongly recommend you read the Rails Guide on Migrations, as well as the section on Mass Assignment which discusses attr_accessible.

To generate a new migration file (since the one mentioned in your Question has already been processed by the previous rake db:migrate command you mentioned running), run

rails g migration AddCategoryIdToProduct category_id:integer

This should generate a new migration with contents like

class AddCategoryIdToProduct < ActiveRecord::Migration
  def change
    add_column :products, :category_id, :integer
  end
end

Running rake db:migrate again now will process this migration file, adding the new category_id integer column to your products table.

deefour
  • 34,974
  • 7
  • 97
  • 90
  • Thanks for the great explenation! But each time i want to add an attribute/property (whatever you call it) to a model i need to use the rails g migration and create a new class? – Danpe Aug 25 '12 at 13:32
  • If the migration's already been applied to the database, **yes**. A model is nothing more than an interface through which you can interact with your database. It does not manipulate the **structure** of your database; that's what migrations are for. Adding a column to a table will automatically make the new attribute methods available within the associated model. – deefour Aug 25 '12 at 13:35
  • Yes. Always create a new migration - even for just one column. Never go back and edit an old migration. It's a good habit! Trust me, your future colleagues will thank me for making sure you follow this practice. – Taryn East Feb 17 '13 at 22:59
7

You can redo a migration by running

rake db:migrate:up VERSION=20121031143418 #insert the timestamp on migration

You can also redo a migration (runs up and down, but only works if you have an up and down, which you won't when you just have a change)

rake db:migrate:redo
kwh941
  • 235
  • 2
  • 9