2011-11-21 3 views
1

보기에 표시되는 일부 레코드가 있습니다. 그들은 start_dateend_date입니다. 보기에 액세스하면 기본적으로 만료되지 않은 레코드 만 표시되기를 원합니다. 가로 정의되는 만료 됨 :레일을 MetaSearch로 날짜순으로 필터링하십시오.

  • 종료 날짜 및 < = 이제 일을 시작하고
  • 종료 날짜가 시작 날짜 이후보다 내가 다음 체크 박스를 갖고 싶어

는 모든 레코드를 보여줍니다 기한이 만료 된 것을 포함하여

어떻게 접근합니까?

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) 
을 컨트롤러 액션에서

답변

0

, 당신은이 곳을 갖고 싶어
관련 문제