Here’s my story: I had something like this set up a while ago.
create_table(:users) do |t|
t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable
t.integer :total_pageviews
and “total_pageviews” worked fine. I could set it to zero, increment it, multiply it, call it, whatever. but let’s say i want to add:
t.integer :total_votes
What do I have to do to configure total_votes as an attribute? Whenever I try to use “total_votes”, I get undefined method error. I’m thinking that the answer is something like rake db:migrate or similar.
You should not ever alter existing migrations. If you have checked them in to your version control system and somebody else runs them and then you add a new field to them and commit that change, they will not know to get that change. Then you must tell them and it’s a pain in the butt to fix.
If you’ve not committed it yet then rollback the migration (
rake db:rollback), make the necessary modification and re-run it again (rake db:migrate).But in the Real World, you would create a new migration which adds this field using
rails g migration add_total_votes_to_users total_votes:integer.Rails will interpret this migration name and know what to do with it, generating a migration that contains this line:
Then when you run this migration it will add this
total_votesfield to theuserstable which will make an attribute of the same name for allUserobjects.