Ruby on Rails: how to migrate changes made on models?
Asked Answered
F

3

13

In a Rails application, how can I migrate the changes I make in models?

For instance, I know that if I create a model with command rails g model Person name:string, a migration will be created as well. However, if after this step I go to the created model Person and add a new attribute, will this new attribute be automatically added to a migration for later persistance in database?

Or am I looking at this from the wrong side, and an attribute should be added to a migration, and then added to a model?

Foredo answered 17/10, 2012 at 23:29 Comment(0)
C
12

You can't really "add" an attribute to a model, you do that by creating the migration file and running it -- Rails figures out what attributes a model has based on what columns are in the database. However, you do need to add a line to the model to whitelist the attribute if you want to be able to update it via mass assignment. That's why you'll often see a line like this in activerecord models:

attr_accessible :name

But that's optional and not essential to adding the attribute.

To actually add the new attribute to your model, first create a migration with:

rails g migration AddAddressToPerson address:string

That will create the migration file in the db/migration/ directory. (The form “AddXXXToYYY” and “RemoveXXXFromYYY” are understood by rails to mean "add (or remove) a new column to the model XXX", see the documentation for details). In this case I've added an attribute named address which is a string, but you could change that to whatever you want it to be.

Then to actually update the database, you need to run the migration with rake:

rake db:migrate

Finally, if you want to allow mass assignment on that attribute, add the attribute to your list of arguments to attr_accessible:

attr_accessible :name, :address

That should do it.

Catania answered 17/10, 2012 at 23:45 Comment(0)
S
2

If you are adding the new attribute with attr_accessor, you will not need to do anything with migrations, but your changes will not be stored in the database.

If you do want to persist your changes, you will need to add the attribute to your model using a migration. You can just create a text file, with the proper structure, migrations are nothing fancy, but it is a lot easier to generate on like this rails generate migration AddLastNameFieldToUsers. The contents of such a file might be adjusted to look like this:

class AddLastNameFieldToUsers< ActiveRecord::Migration
  def change
    add_column :users, :last_name, :string
  end
end
Sachikosachs answered 17/10, 2012 at 23:43 Comment(0)
E
0

You don't need to add attributes directly to the model. Rails (actually ActiveRecord) infers it automatically. For the list of attributes for the model class, AR looks for a table with a plural form of the model's name (if model is Order, then it will look for attributes in the orders table). It is part of the design feature called CoC - Convention over Configuration.

So you if you have to add an attribute, you have to create a migration to add that field into the column as mentioned in other answers.

Enclosure answered 18/10, 2012 at 4:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.