簡體   English   中英

活動記錄中的屬性比較

[英]comparison of attributes in active record

在我的rails應用程序中,我有兩個模型Student和StudentRecord。 student.rb has_many:student_records

在student_records_controller中,我想搜索所有具有如此多查詢參數的學生的所有記錄。 學生記錄的兩個屬性是date_recorded和status。

因此,在一次特定的搜索中,我希望獲得狀態為“失敗”的學生的最新記錄,但前提是該記錄后面沒有“通過”記錄。

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

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM