I have 2 models
class User < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :user
end
Do I need to add a column user_id to the Product table or does rails add it by default ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You do need to manually add the
user_idcolumn to theProductmodel. If you haven’t created your model yet, add the reference in the column list to the model generator. For example:rails generate model Product name:string price:decimal user:referencesOr, if your
Productmodel already exists what you have to do is:rails g migration addUserIdToProducts user_id:integerThat will generate a migration that properly add the
user_idcolumn to theproductstable. With the column properly named (user_id), Rails will know that’s your foreign key.