Forum: Ruby on Rails Search screens with dates

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0cffe192be5af0fca5bdbd73cf6c82ea?d=identicon&s=25 Ian J Cottee (Guest)
on 2005-12-12 19:40
(Received via mailing list)
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
0cffe192be5af0fca5bdbd73cf6c82ea?d=identicon&s=25 Ian J Cottee (Guest)
on 2005-12-14 14:32
(Received via mailing list)
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

<h1>Search Signals</h1>
<%= start_form_tag :action => :list %>
<p>

     <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
This topic is locked and can not be replied to.