The Traditional Way
Here I have a table of change records in my rails app. And I have added a query for created_at with ransack.
1 2 3 4 5 6 |
|
1 2 3 4 5 |
|
The Problem
Everything works fine until users start to use it. They are surpised that, when query with “2015-01-01” and “2015-01-01”, nothing comes out.
Certainly there’s nothing between ‘2015-01-01 00:00’ and ‘2015-01-01 00:00’. But our users don’t think so. They shout that there is a whole day from 2015-01-01 to 2015-01-01!
Direct solution
OK. Users are gods. So I add some codes in my controller:
1 2 3 4 5 6 7 8 |
|
The created_at_lteq will convert to ‘2015-01-01 23:59’.
DRY
I customed the ransack predicates to avoid duplication. So I can just write the view like following:
Maybe Another Way
Maybe we can change the js datepicker to set time to 59:59 by default. I use bootstrap-datetimepicker. I find the javascript solution:
= hidden_field_tag 'q[created_at_lteq]', params[:q].try(:[], :created_at_lteq)
= date_field_tag '', params[:q].try(:[], :created_at_lteq).to_s.sub(/ .+/, ''), onchange: "$(this).prev().val($(this).val() != '' ? $(this).val() + ' 23:59:59' : '')"
One Thing More
Following make query in one day more convenient.
1 2 3 4 5 6 7 |
|