Ruby on rails 具有多个连接的ActiveRecord查询无法识别关系

Ruby on rails 具有多个连接的ActiveRecord查询无法识别关系,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我正在尝试编写一个ActiveRecord查询,该查询使用以下查询返回某一课程中注册的所有学生: def self.students_enrolled_in(course_id) Student .joins(:enrollments) .joins(:sections) .joins(:courses) .where(sections: { course_id: course_id }) end rails控制台中的

我正在尝试编写一个ActiveRecord查询,该查询使用以下查询返回某一课程中注册的所有学生:

def self.students_enrolled_in(course_id)
    Student
        .joins(:enrollments)
        .joins(:sections)
        .joins(:courses)
        .where(sections: { course_id: course_id })
  end
rails控制台中的结果是:

ActiveRecord::ConfigurationError:无法将“学生”加入名为“节”的关联;也许你拼错了

看来这个协会成立了。我做错了什么?这个查询实际上是否意味着所有的
join()
语句都必须与Student相关,或者ac应该跟踪关系链接

教授展示页面:

<div class="col-md-8">
  <h2 class="card-title"><%= @professor.name %></h2>

    <% @courses_taught.each do |course| %>
        <div class="card mb-4 card-header">
          <img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap">
          <h3 class="card-text"><%= course.title %></h3>
        </div>
        <div class="card-body">
          <% course.sections.enrollments.students.each do |student| %>
              <p><% student.name %></p>
          <% end %>
        </div>
    <% end %>

</div>

您过度应用了
.joins
。试着从里到外开始。首先,找到课程:

Course.find_by(id: course_id)
然后,查找与
课程相关的所有部分。无需在此处执行
连接操作

Section.where(course: Course.find_by(id: course_id))
现在您可以加入:

Student.joins(:enrollments).where(enrollments: {section: Section.where(course: Course.find_by(id: course_id))})
我想这应该对你有好处。但是,没有经过测试。所以,试一试,看看它是否有效


注意:试着只发布最相关的代码。整理一大堆无关的东西并不是那么有趣

另一种方法是向您的
学生
模型添加更多关联:

class Student < ApplicationRecord
  has_many :enrollments
  has_many :sections, through: :enrollments
  has_many :courses, through: :sections

  scope :enrolled_in_course, -> (course) { joins(:sections).where(course_id: course.id)
end

谢谢,我现在就来测试。我不确定在这种情况下什么是相关的,如果知道实体之间的关系是否相关,那么就把它们包括在内。
ActiveRecord::Schema.define(version: 20171013201907) do

  create_table "courses", force: :cascade do |t|
    t.string "title"
    t.text "description"
    t.string "number"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "status", default: 0
    t.integer "department_id"
    t.index ["department_id"], name: "index_courses_on_department_id"
  end

  create_table "departments", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.text "main_image"
    t.text "thumb_image"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "enrollments", force: :cascade do |t|
    t.integer "section_id"
    t.integer "student_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["section_id"], name: "index_enrollments_on_section_id"
    t.index ["student_id"], name: "index_enrollments_on_student_id"
  end

  create_table "professors", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "status", default: 0
    t.integer "department_id"
    t.text "bio"
    t.index ["department_id"], name: "index_professors_on_department_id"
  end

  create_table "sections", force: :cascade do |t|
    t.integer "number"
    t.integer "max_enrollment"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "professor_id"
    t.integer "course_id"
    t.string "room"
    t.index ["course_id"], name: "index_sections_on_course_id"
    t.index ["professor_id", "course_id"], name: "index_sections_on_professor_id_and_course_id", unique: true
    t.index ["professor_id"], name: "index_sections_on_professor_id"
  end

  create_table "students", force: :cascade do |t|
    t.string "name"
    t.decimal "gpa"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "name"
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "roles"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end
Course.find_by(id: course_id)
Section.where(course: Course.find_by(id: course_id))
Student.joins(:enrollments).where(enrollments: {section: Section.where(course: Course.find_by(id: course_id))})
class Student < ApplicationRecord
  has_many :enrollments
  has_many :sections, through: :enrollments
  has_many :courses, through: :sections

  scope :enrolled_in_course, -> (course) { joins(:sections).where(course_id: course.id)
end
Student.enrolled_in_course(course)