获取通过 Rails 中的值列表连接的所有行

Fetch all rows that join via a list of values in Rails

我有两个 tables(models):

  1. 学生
  2. 课程

它们都通过名为 course_enrollments

的 table 连接

在 rails 中,它们的设置如下:

Student.rb:
has_many :courses, through: course_enrollments

Course.rb:
has_many :students, through: course_entrollments

CourseEntrollment.rb:
belongs_to :student, foreign_key...
belongs_to :course, foreign_key..

我想从特定的课程列表中获取 all 注册 all 课程的学生,如下所示:

courses_list = current_professor.courses_teaching
student_list = Student.eager_load(:some_table_I_have_to_load).join_with_all_courses(courses_list).order(sort_attributes).paginate....

我的问题是我该怎么做?我在 SQL 方面不太强,我正在努力弄清楚我需要什么。 我认为我需要在 order 之前放置某种连接或 where 子句,但我很难弄清楚什么以及如何正确地做到这一点

非常感谢!

如果courses_list是一个关系。做起来很简单:

student_list = Student.joins(:course_enrollments).where(course_enrollments: { course_id: courses_list.ids })