I have a model “List” with an attribute called “description” of type string. Not a good idea so I want to change it to type text. However, I can’t change it.
I’ve done:
rails destroy model List
rake db:rollback
rake db:migrate
I then re-declare the model with description of type text and do a rake db:migrate.
Then I do:
rails c
ruby-1.9.2-p290 :002 > List
=> List(id: integer, name: string, popularity: string, vote_counter: integer, description: string, created_at: datetime, updated_at: datetime)
As you can see, description is still of type string.
I look in the migration and it’s of type text.
def change
create_table :lists do |t|
t.string :name
t.string :popularity
t.integer :vote_counter
t.text :description
t.timestamps
end
end
How do I actually change it to text?
Thanks.
The problem is that “destroy model” removed your migration script. So rollback is not going to rollback your original model because there is no migration script for it to rollback. When you ran rake db:rollback, you didn’t really see the drop_table(:lists), did you? You should have not called destroy model just because you want to change the model.
What you should have done is:
1. rake db:rollback
2. Change your migration script
3. rake db:migrate
The problem is that now your database still has the old table. So you need to manually remove your table from the database and run rake db:migrate again. That should solve your problem.