Reputation: 45
My problem is when user selects same dates, query below returns empty array. Instead when user selects same dates, it has to show users created at that date. Any help will be appreciated.
User.where("created_at >= ? and created_at =< ?", params[:from].to_date, params[:to].to_date)
Upvotes: 3
Views: 1023
Reputation: 2797
Another way to achieve this:
params[:from].to_date.beginning_of_day and params[:to].to_date.end_of_day. This will cover the whole day
Upvotes: 3
Reputation: 25766
You are probably using DATETIME database columns. The problem is that the user selects "from" as (for example) 2011-07-09 and for the comparison the database translates that to "2011-07-09 00:00:00" and if the user selects the same day twice, there will probably be no results (unless they were exactly created at "2011-07-09 00:00:00"). To fix this, just do
to_date = params[to].to_date + 1.day
before making the actual database search call. And use to_date instead of params[:to].to_date
Upvotes: 1