Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: how to migrate changes made on models?

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?

Regards

like image 692
Rui Avatar asked Oct 17 '12 23:10

Rui


People also ask

What is difference between model and migration in rails?

Rails Model (Active Record) works with SQL, and Rails Migration works with DDL. Rails Model supports ways to interact to with the database, while Rails Migration changes the database structure. A migration can change the name of a column in books table.

How do I migrate a specific migration in rails?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.


2 Answers

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.

like image 102
Chris Salzberg Avatar answered Oct 23 '22 07:10

Chris Salzberg


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
like image 33
Brad Werth Avatar answered Oct 23 '22 05:10

Brad Werth