Forum: Ruby on Rails Twice select_date -> Date range

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.
Mark N. (Guest)
on 2007-01-16 00:25
Hi all,

I'm using twice the select_date function in my view to select a date
from and a date until for a date property in my model called
"written_on".

<p><label for="proposal_written_from">Written between</label><br/>
<%= select_date nil, :prefix => "written_from",
  :order => [:day, :month, :year],
  :include_blank => true %></p>

<p><label for="proposal_written_until">and</label><br/>
<%= select_date Date.today, :prefix => "written_until",
  :order => [:day, :month, :year],
  :include_blank => true %></p>

In my model, I want to create a date range if the user has selected
valid values for both the date written_from and the date written_until.
The POST parameters for written_from and written_until look like this:

Parameters: {"commit"=>"Find", "written_until"=>{"month"=>"1",
"day"=>"20", "year"=>"2007"}, "action"=>"find_proposals",
"controller"=>"proposals", "proposal"=>{"client_firstname"=>"",
"client_surname"=>"", "city"=>"", "box"=>"", "postal_code"=>"",
"number"=>"", "units"=>"", "policy_number"=>"", "street"=>"",
"agent_id"=>""}, "written_from"=>{"month"=>"1", "day"=>"2",
"year"=>"2007"}}

Basically I just want to create a range like (date_from..date_until)
that I can use with the ez_where plugin for a SQL BETWEEN condition.

I tried to create the dates like this:

written_from = Date.civil(p[:written_from][:year],
p[:written_from][:mon], p[:written_from][:day])
written_until = Date.civil(p[:written_until][:year],
p[:written_until][:mon], p[:written_until][:day])

but that doesn't seem to work. The ez_where condition would look
something like:

cond = Condition.new do
  written_on <=> (date_from..date_until) unless date_from.nil? ||
date_until.nil?
end

Can anybody help me out with this? The ez_where plugin can be found at
http://brainspl.at/articles/2006/01/30/i-have-been-busy

Thanks for your reply,

Mark
This topic is locked and can not be replied to.