2015-05-04 80 views
2

在我的rails应用程序中,我有两个模型Student和StudentRecord。 student.rb has_many:student_records活跃记录中属性的比较

in student_records_controller我想搜索所有查询参数的所有学生的所有记录。学生记录的两个属性是date_recorded和状态。

因此,在一个特定的搜索中,我希望获得状态为“失败”的学生的最新记录,但前提是记录后没有“通过”记录。

scope = StudentRecord.join(:student) 
     . 
     . 
     . 
#so a query something like the following 
scope.where("status = ?", params[:search][:status]).having .... 
+0

您正在使用什么数据库? –

+0

我正在使用Postgres – Tiamon

回答

0

@Tiamon是可以在内存中筛选从数据库中检索的记录后? 是这样,你也许可以做到这样说:

可能需要进一步优化

student_records = StudentRecord.join(:student).order('students_records.date_recorded')

found_records = student_records.each_with_index do |student_record, index| if student_record.state == 'fail' if ((student_records[index + 1] && student_records[index +1].state != 'pass') || !student_records[index + 1]) student_record end end

+0

这将包括所有曾经失败的学生 – Albin

+0

@albin是正确的,我误解了这个问题。 – cnicolaou

+0

有人指出,该记录属于特定用户“我想获得学生的最新记录” – cnicolaou