Can you do greater than comparison on a date in a Rails 3 search?
Note.
where(:user_id => current_user.id, :notetype => p[:note_type]).
where("date > ?", p[:date]).
order('date ASC, created_at ASC')
or you can also convert everything into the SQL notation
Note.
where("user_id = ? AND notetype = ? AND date > ?", current_user.id, p[:note_type], p[:date]).
order('date ASC, created_at ASC')
If you hit problems where column names are ambiguous, you can do:
date_field = Note.arel_table[:date]
Note.where(user_id: current_user.id, notetype: p[:note_type]).
where(date_field.gt(p[:date])).
order(date_field.asc(), Note.arel_table[:created_at].asc())
You can try to use:
where(date: p[:date]..Float::INFINITY)
equivalent in sql
WHERE (`date` >= p[:date])
The result is:
Note.where(user_id: current_user.id, notetype: p[:note_type], date: p[:date]..Float::INFINITY).order(:fecha, :created_at)
And I have changed too
order('date ASC, created_at ASC')
For
order(:fecha, :created_at)
If you aren't a fan of passing in a string, I prefer how @sesperanto has done it, except to make it even more concise, you could drop Float::INFINITY
in the date range and instead simply use created_at: p[:date]..
Note.where(
user_id: current_user.id,
notetype: p[:note_type],
created_at: p[:date]..
).order(:date, :created_at)
Take note that this will change the query to be >=
instead of >
. If that's a concern, you could always add a unit of time to the date by running something like p[:date] + 1.day..