Reputation: 17589
Do I need to re-migrate whenever I change the models in Rails? Can someone tell me what migration really does? I am reading it and I am somewhat confused with the difference between the stuff inside db/migrate and the stuff inside app/models.
For example, if I add a has_one realtionship inside my model, do I need to re-migrate this? Why?
Upvotes: 1
Views: 866
Reputation: 40277
If your database changes, use a migration. If you're just adding methods to your model, no need to have a migration.
Example:
We start out and we just have first_name, last_name. We want to store those in the database, so we have a migration that does:
/app/models/human.rb
# empty
/db/migrate/xxxxx.rb
add_column :humans, :first_name, :string
add_column :humans, :last_name, :string
Then we get married, so we want to track that
/app/models/human.rb
belongs_to :spouse
/db/migrate/xxxxx.rb
add_column :humans, :spouse_id, :integer
/app/models/offspring.rb
belongs_to :human
/db/migrate/xxxxx.rb
create_table ...
/app/models/human.rb
has_many :offspring
/app/models/human.rb
def first_born
offspring.first
end
Upvotes: 5