2015-05-04 77 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