title: ActiveRecord query with multiple joins not recognizing the relations
body: <p>I am trying to write an ActiveRecord Query that returns all students enrolled in a certain course with the following query:</p>
  
  <pre><code>def self.students_enrolled_in(course_id)
      Student
          .joins(:enrollments)
          .joins(:sections)
          .joins(:courses)
          .where(sections: { course_id: course_id })
    end
  </code></pre>
  
  <p>the result in the rails console is:</p>
  
  <blockquote>
    <p>ActiveRecord::ConfigurationError: Can't join 'Student' to association named 'sections'; perhaps you misspelled it?</p>
  </blockquote>
  
  <p>it seems that the association is made. what am I doing wrong? does the query actually mean that all the <code>join()</code> statements have to relate back to Student, or should ac trace out the relational links? </p>
  
  <p>Professor show page:</p>
  
  <pre><code>&lt;div class="col-md-8"&gt;
    &lt;h2 class="card-title"&gt;&lt;%= @professor.name %&gt;&lt;/h2&gt;
  
      &lt;% @courses_taught.each do |course| %&gt;
          &lt;div class="card mb-4 card-header"&gt;
            &lt;img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap"&gt;
            &lt;h3 class="card-text"&gt;&lt;%= course.title %&gt;&lt;/h3&gt;
          &lt;/div&gt;
          &lt;div class="card-body"&gt;
            &lt;% course.sections.enrollments.students.each do |student| %&gt;
                &lt;p&gt;&lt;% student.name %&gt;&lt;/p&gt;
            &lt;% end %&gt;
          &lt;/div&gt;
      &lt;% end %&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>models:</p>
  
  <p>enrollment</p>
  
  <pre><code>class Enrollment &lt; ApplicationRecord
    belongs_to :section
    belongs_to :student
  end
  </code></pre>
  
  <p>Student:</p>
  
  <pre><code>class Student &lt; ApplicationRecord
    has_many :enrollments
  end
  </code></pre>
  
  <p>Professor:</p>
  
  <pre><code>class Section &lt; ApplicationRecord
    has_many :enrollments
    belongs_to :professor
    belongs_to :course
  
    validates_uniqueness_of :professor_id, scope: :course_id
  
    scope :by_professor_id, -&gt;(prof_id) { where('professor_id = ?', prof_id) }
  end
  </code></pre>
  
  <p>Course:</p>
  
  <pre><code>class Course &lt; ApplicationRecord
    enum status: { planning: 0, offered: 1 }
  
    scope :offered, -&gt; { where(status: 1) }
    scope :planning, -&gt; { where(status: 0) }
  
    belongs_to :department
    has_many :sections
    has_many :professors, through: :sections
  
    validates :title, :number, :status, :description, presence: true
    validates :description, length: { in: 10..500 }
    validates :title, :number, uniqueness: { case_sensitive: false }
  
    def self.search(term)
      if term
        where('title LIKE ?', "%#{term}%").order('title DESC')
      else
        order('title ASC')
      end
    end
  
    def self.taught_by(professor_id)
      Course
          .joins(:sections)
          .joins(:professors)
          .where(sections: { professor_id: professor_id })
          .select('distinct courses.*')
    end
  
  end
  </code></pre>
  
  <p>Schema:</p>
  
  <pre><code>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
  </code></pre>