title: Compare the last element in Rails active record
body: <p>I have two models Student and StudentRecord. Student Record has start_date, end_date and class_course_id among its attributes and it belongs to Student</p>
  
  <pre><code>scope = Student.eager_load(:student_record)
  </code></pre>
  
  <p>Now, I want to get the students whose latest(according to start_date) student_record's class_course_id is the same as a given class_course_id. something like:</p>
  
  <pre><code>scope = scope.where("student_records.order(start_date asc).last.class_course_id = ?", params[:class_course_id])
  </code></pre>
  
  <p>Obviously, the above statement is not correct but I hope it describes what I would like to get.</p>