How do polymorphic associations work in Rails? What are their advantages? Is there a way to add belongs_to method just by running a migration?
How do polymorphic associations work in Rails? What are their advantages? Is there a
Share
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.
Ryan has a railscast about this that is pretty good.
Belongs_to isn’t something you add to a migration, you add it to the model. In the migration, you have to add the foreign key column. For example if you have a post model that belongs to a user, you’d add the user_id column to the post activerecord in a migration. Then you add
in the post model separately. Then rails will do its magic in the background to give you the proxy collections in the user model.