Sorry for the basic, basic question but I’m having some trouble understanding the RoR doc.
say I have 3 models–Students, Classes, and Enrolled_in. If it isn’t clear, students will enroll in classes, so Enrolled_in should have Students and Classes as foreign keys. I generated the models for each of these, but I’m confused what I should put into the associated migration file vs. the associated model file for each table. Do I specify the columns of the table in the migration file, and the key constraints in models?
If someone could clarify this, or tell me how they would solve the example question I posted, that would be really helpful. Thanks.
Firstly, to follow the convention the model name should be
EnrolledIn, notEnrolled_in. Better yet, change it toEnrollmentor something that has a definite meaning as a noun. And you’re also going to run into trouble trying to useClass(which is already a ruby object) as a model name. Perhaps change it toCourseor something similar.That aside, you should define all columns in your migrations. You can define the
enrolled_instable like this:The
referencesshortcut will add a :student_id and :course_id as integer fields.In your model files you’ll have: