简体   繁体   中英

Rails 4 - thinking-sphinx date range

I have a model object which have a start and end date. When searching I would like to add a condition where I only get objects that have a start_date that is before Time.now and an end_date the is after Time.now , that is objects that are ongoing .

I have tried the following:

@team = Team.find_by_id(params[:team_id])

# start_date and end_date are properties on my model object, that I want to filter based on.
@competitions = @team.competitions.search(params[:search], conditions: { :start_date => start_date..Time.now.to_i, :end_date => end_date..Time.now.to_i }, :page => params[:page], :per_page => 10, :order => 'created_at DESC')

This results in the following error:

undefined local variable or method `start_date'

Any ideas on what I should do to get my wanted behaviour?

Perhaps something like this will do the trick:

@competitions = @team.competitions.search params[:search],
  :select   => '*, start_date < NOW() AND end_date > NOW() as current',
  :with     => {:current => true},
  :page     => params[:page],
  :per_page => 10,
  :order    => 'created_at DESC'

This generates a custom attribute on the fly ( current ) and then filters by that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM