How do you modify a model you've generated using modeling? For example, the model myModel originally had columns a, b and c, but I now want to add column d.
+7
A:
The best answer I've found so far is run this from your project root:
ruby script/generate migration add_d_column_to_myModel
Then edit the new migration file located in db/migration to look something like:
def self.up
add_column :myModel, :d, :string
end
def self.down
remove_column :myModel, :d
end
The last step will be to update your views accordingly.
Answer found here
Table functions found here
Andrew Hampton
2009-02-10 02:05:19
Confused--why did you ask the question if you had a good answer? Why not post this in the original question and ask for better solutions?
zenazn
2009-02-10 02:10:55
I think you answered your own question. One thing I will say, however, is that in some case you can edit the model migration directly. Migrations are great when you have a site in production, but for ongoing development, you often have the luxury of blowing away the DB and starting again.
Toby Hede
2009-02-10 02:12:02
I answered my own question for 2 reasons. First, this was the best answer I found, but I'm new to Rails and thought there may be a better way I didn't find. Second, the Question hadn't been posted on StackOverflow, so I decided to add it.
Andrew Hampton
2009-02-10 11:51:10
+5
A:
ruby script/generate migration add_fieldname_to_tablename fieldname:string
this is the shortcut method to do exactly what you want. if you need more control, or if you have a lot of columns to add, the method above will work fine too.
Luke
2009-02-10 02:08:45