Reputation: 1000
so I'm new to Ruby, so naturally I'm starting off with Rails 4. So the problem is that I've already created my models with scaffolds, and now I'm trying to figure out how to add attributes to a model that already exists. Say, I have a person, and I forgot to create the person_name attribute and now I want to add it.
Do I do this?
How does one add an attribute to a model?
Or is there some other way in rails 4?
Upvotes: 2
Views: 6924
Reputation: 22296
If you are using the Rails 4.x you can now generate migrations with references, like this:
rails generate migration AddUserRefToProducts user:references
like you can see on rails guides
Upvotes: 0
Reputation: 3265
Yep, execute the command mentioned in answer #2, something like:
rails g migration AddAttributeToModel attribute_name:datatype
Where "Attribute", "Model", and "attribute_name" are the names of the attribute and model in question and "datatype" would be "string", "boolean", etc.
The attribute is added to your schema when you run "rake db:migrate" again.
By the way, the above example adds one new attribute, but you can add multiple new attributes at once simply by passing additional "attribute_name:datatype" pairs to the end of the command. Also, if you specify only the attribute name and leave off the ":datatype", it will default to string.
Upvotes: 11