Search screens with dates


#1

Tried googling and searching the lists for this. I’d like to do a
search on some dates and naturally enough use the date helpers. I
have a number of problems:

– datetime_select wants an active record model
– select_datetime does not take the options that datetime_select
take - specifically :order

that leaves me with something like

     <label>Date/Time To:</label><br />
     <%= select_day time = Date.today, {:prefix => 'to'} %>
     <%= select_month  Date.today, {:prefix => 'to'} %>
     <%= select_year Date.today, {:prefix => 'to'} %>
     <%= select_hour Time.now, {:prefix => 'to'} %>
     <%= select_minute Time.now, {:prefix => 'to'} %>

which I can just about live with. But how do I easily take those five
fields and place into a Model.find(:all, conditions => “search for
date less then the given params”) type query?

Is there an easy way?

Ian


#2

In the absence of a reply I ended up with the following in case
anybody else gets as stuck as I did.

On the form I have

Search Signals

<%= start_form_tag :action => :list %>

 <label>Date/Time From:</label><br />
     <%= select_day Date.today, {:prefix => 'from'} %>
     <%= select_month Date.today, {:prefix => 'from'} %>
     <%= select_year nil, {:prefix => 'from'} %>
     <%= select_hour nil, {:prefix => 'from'} %>
     <%= select_minute nil, {:prefix => 'from'} %>
 </p>
 <p>
     <label>Date/Time To:</label><br />
     <%= select_day time = Date.today, {:prefix => 'search_to'} %>
     <%= select_month  Date.today, {:prefix => 'search_to'} %>
     <%= select_year Date.today, {:prefix => 'search_to'} %>
     <%= select_hour Time.now, {:prefix => 'search_to'} %>
     <%= select_minute Time.now, {:prefix => 'search_to'} %>
 </p>

 <%= submit_tag 'Search'%>

<%= end_form_tag %>

On the controller I have

def list

 s = 'True ' # This will be our search string
 p = Hash.new # Parameters for our search string


 if @params[:search_from]
 	  s += "and datepoint >= :search_from "
   p[:search_from] = sql_date(@params[:search_from])
 end

 if @params[:search_to]
 	  s += "and datepoint <= :search_to "
  p[:search_to] = sql_date(@params[:search_to])
 end

 @raw_signal_pages, @raw_signals =

paginate :raw_signal, :per_page => 30, :conditions => [s, p], :order_by => “id desc”

end

And finally in application.rb I have the following little script
which takes the date/time field helper hash and makes a valid
searchable datetime from it.

def sql_date(date_hash)

search_string = "%s/%s/%s %s:%s" %
	[
		date_hash[:year],
		date_hash[:month],
		date_hash[:day],
		date_hash[:hour],
		date_hash[:minute]
	]

end