Using join tables in ruby on rails

前端 未结 3 1667
难免孤独
难免孤独 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

    0 讨论(0)
  • 2020-12-04 17:25

    Yes, this is a many-to-many relationship (class has many students, student has many classes). For this you'll use a has_many :through relation. Take a look at the docs for ActiveRecord::Associations (Ctrl-F for "Association Join Models").

    In a migration, t.references :students is how you would specify a belongs_to relation, as it just adds a student_id column (which can only accommodate one id, i.e. one student). A join model, however, will have two columns: student_id and class_id. (Incidentally, calling a model 'Class' in Ruby is asking for trouble. Might I suggest 'Course'?)

    0 讨论(0)
  • 2020-12-04 17:43

    Everything true what @Jordan said, here the concrete steps to take:

    1. Create a migration: rails g model CourseStudent creates a join model for the n:m relation, and the migration to the corresponding table.
    2. Edit the migration file CreateCourseStudent so it contains the following:

      class CreateCourseStudent < ActiveRecord::Migration
        def change
          create_table :course_students do |t|
      
            # Your code comes here
            t.integer :student_id
            t.integer :course_id
      
            # Here comes the generated code 
            t.timestamps
          end
        end
      end
      
    3. Run the migration: rake db:migrate. As a result, the join table should now exist in your database.

    4. Add to the models the following code

      class Course < ActiveRecord::Base
        has_many :course_students
        has_many :students, :through => :course_students
      end
      
      class Student < ActiveRecord::Base
        has_many :course_students
        has_many :courses, :through => :course_students
      end
      
      class CourseStudent < ActiveRecord::Base
        belongs_to :student
        belongs_to :course
      end
      

    You are now able to use the methods generated by the methods belongs_to and has_many:

    • @course.students
    • @student.courses

    Try to find all the relevant facts and snippets in the Rails Guides, there you should find all information you need to get on track. Good luck!

    0 讨论(0)
提交回复
热议问题