views:

674

answers:

2

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?

A: 

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

Ryan Bigg
+1  A: 

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
egarcia