1

The Rails plugin - Searchlogic, from binary logic - offers the ability to filter by date. I have my form set up like so...

<% form_for @search do |f| %>
    <%= f.label :start %> <%= f.select :due_at_after, [[['','']],['November', '2009-11-01'.to_date],['December', '2009-12-01'.to_date]]  %><br>
    <%= f.label :end %> <%= f.select :due_at_before, [[['','']],['December', '2009-12-01'.to_date],['January', '2010-01-01'.to_date]]  %>
    <%= f.submit 'Search' %>
<% end %>

But, the date I'm getting back from the @search object as generated in the controller...

@search = Task.with_member(current_user).search(params[:search])

is generating a date param that looks like this

{:due_at_after=>Sat, 31 Oct 2009 20:00:00 EDT -04:00}

With the two formats, the form will not show the drop-down as selected. It looks as if the format in the searchlogic object uses a timezone adjusted time, too.

Any thoughts on how to handle this?

Mark Swardstrom
  • 17,217
  • 6
  • 62
  • 70
  • I found that the calendar_date_select gem works, yet still with some problems. The date format changes, but in the text field, that's less of a problem than with a select box. Still looking for the right way to do this. – Mark Swardstrom Dec 02 '09 at 06:09
  • see also: http://stackoverflow.com/questions/2280980/searchlogic-doesnt-convert-the-time-properly-for-datetime-conditions – reto Feb 17 '10 at 13:45

2 Answers2

1

A big part of your issue seems to be happening because you are converting strings into dates, and dates back to strings. I believe you might be doing it more than you need to.

HTML forms don't really "understand" dates - they just "understand" strings. So it is ok to pass them strings instead dates. In other words, it is ok to remove the to_date.

<% form_for @search do |f| %>
    <%= f.label :start %>
    <%= f.select :due_at_after,
          ['November', '2009-11-01'],['December', '2009-12-01']],
          :include_blank => true
    %>
    <br/>
    <%= f.label :end %>
    <%= f.select :due_at_before,
          [['December', '2009-12-01'],['January', '2010-01-01']],
          :include_blank => true
    %>
    <%= f.submit 'Search' %>
<% end %>

Also, I prefer using :include_blank => true instead of [['','']] (more human-readable, in my opinion), and I used a closed <br/> tag (standard html stuff - maybe you made a typo?).

By the way, if you want to specify a Date, you can use a Date constructor. It is shorter to write and faster to execute than creating a string and parsing a date from it.

#Date(2009,11,1) is faster, shorter, and equivalent
Date(2009,11,1) == '2009-11-01'.to_date # --> true
kikito
  • 51,734
  • 32
  • 149
  • 189
0

Are you looking to find records for a given month? Check out my by_star gem/plugin for this.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261