Reputation: 8497
I have the following model:
class Product < ActiveRecord::Base
end
How can I add a field to it?
Upvotes: 1
Views: 452
Reputation: 3818
What is the type of the field you wish to add? If it is a string do it like below:
rails generate migration add_field_to_products field:string
Where 'field' is the name of the field you want to add, so rename accordingly.
Then do in the command line rake db:migrate
and after that the field should be in the model.
Also, this question is a duplicate of: Adding a column to an existing table in a Rails migration . There is more discussion of this there if it's still unclear.
Upvotes: 2
Reputation: 44601
You should generate a separate migration with rails g migration [name]
, where you declare your column:
add_column :products, :[column_name], :[datatype]
Where [column_name]
is the name of the column you want to add and [datatype]
stands for it's datatype: string, integer and etc.
After that you should run migration with rake db:migrate
to add new column to your table.
Upvotes: 0