Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ahmed Mohamed Fouad
11,735 PointsHow did models knew that "post_id" in comments model will be the foreign key for the primary key "id" in posts model ?
How did models knew that "post_id" in comments model will be the foreign key for the primary key "id" in posts model ?
is it from adding index when we were adding the "post_id" column to comments model ? or ... ?
1 Answer
Jay McGavren
Treehouse TeacherFrom the ActiveRecord documentation on belongs_to:
By default this is guessed to be the name of the association with an
_idsuffix. So a class that defines abelongs_to :personassociation will use"person_id”as the default:foreign_key.
If you want to use a different column name as the foreign key, you can pass the foreign_key when you call the belongs_to method.