Using join tables in ruby on rails

前端 未结 3 1674
难免孤独
难免孤独 2020-12-04 16:43

Lets say I have two databases: one for students and one for classes. I would like to be able to \'add\' classes to a specific student and also be able to add students to a

3条回答
  •  独厮守ぢ
    2020-12-04 17:17

    This is an old question, but just in case anyone stumbles upon this like I did, you can now have the relationships has_and_belongs_to_many. So yes, you would create a join table:

    create_join_table :students, :courses do |t|
      t.integer :student_id
      t.integer :course_id
    end
    

    And then in the models, you would say that a student has_and_belongs_to_many :courses And a course has_and_belongs_to_many :students. There is no need to make a third class called CourseStudent. This link has all this information

提交回复
热议问题