2011-11-21 95 views
1

我有一些记录,我在视图中显示。他们有start_dateend_date。 当我访问视图时,默认情况下我只想显示记录谁的日期没有过期。 过期被定义为:使用MetaSearch按日期过滤轨道

  • 结束日期和开始日期< =现在,和
  • 结束日期晚于开始日期

然后我想有一个复选框,显示所有记录包括那些已经过期的。

我该如何解决这个问题?

回答

0

在你的控制器动作,你想拥有这样的地方:

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)