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.