簡體   English   中英

Rails使用MetaSearch按日期過濾

[英]Rails filter by date with MetaSearch

我有一些在視圖中顯示的記錄。 它們具有start_dateend_date 訪問視圖時,默認情況下,我只希望它顯示誰的日期未過期的記錄。 過期被定義為:

  • 結束日期和開始日期<=現在,和
  • 結束日期晚於開始日期

然后,我希望有一個復選框顯示所有記錄,包括已過期的記錄。

我該如何處理?

在控制器操作中,您希望將其放置在某處:

params[:search] ||= {} # make sure this is instantiated

# filter to show the expired records
if params[:include_expired] # check if the checkbox is checked
  params[:search][:end_date_lte] = Date.today # end_date <= Now
  params[:search][:start_date_lte] = Date.today # end_date <= Now
end

@records = params[:include_expired] ? RecordClass.where("end_date > start_date").search(params[:search]) : RecordClass.search(params[:search])

您還可以繞過所有搜索內容,而僅使用如下范圍:

@records = params[:include_expired] ? RecordClass.expired : RecordClass.all # or whatever logic you need

# scope in the RecordClass
scope :expired, where("end_date > start_date AND end_date <= ?", Date.today)

暫無
暫無

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

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