如果你想要所有的學生,那麼這在活動記錄中有點不平凡。
確定最後的學生記錄聽起來會從受益範圍一件重要的事情:
def self.latest_for_student
where("not exists (select null from student_records sr2 where sr2.student_id = student_records.student_id and sr2.start_date > student_records.start_date)")
end
...這意味着「返回對此有不student_records存在另一行的行爲同一student_id數據和更大的起始日期「
或者......
def self.latest_for_student
where("student_records.start_date = (select max(start_date) from student_records sr2 where sr2.student_id = student_records.student_id)")
end
...這意味着」返回行爲其起始日期是等於最高
class Student
has_one :last_student_record, -> {merge(StudentRecord.latest_for_student)}, :class_name => "StudentRecord"
has_one :last_class_course, :through => :last_student_record, :source => :class_course
end
class ClassCourse
has_many :last_records_for_student, -> {merge(StudentRecord.latest_for_student)}, :class_name => "StudentRecord"
has_many :students_having_as_last_course, :through => : last_records_for_student, :source => :student
end
那麼你應該能夠:您可以爲學生證」
然後imum開始日期
@course.students_having_as_last_course
有點複雜......可能是語法錯誤......讓我知道如果有。
不,這不起作用。我認爲在它已經取得一場比賽之後,它會命令它。 – Tiamon