Which is the most ruby style way to do this? Rubies only

If i have a select_date in my view like this :

select_date @end_date,:prefix=>“end_date”

Then I parse it as follows in the controller:

@end_date =
Time.parse(params[“start_date”][:day]+"-"+params[“start_date”][:month]+"-"+params[“start_date”][:year]).to_date

This somehow doesn’t seem right. What is the best method to do it?

Thanks
Chris

Chris wrote:

If i have a select_date in my view like this :

select_date @end_date,:prefix=>“end_date”

Then I parse it as follows in the controller:

@end_date =
Time.parse(params[“start_date”][:day]+"-"+params[“start_date”][:month]+"-"+params[“start_date”][:year]).to_date

This somehow doesn’t seem right. What is the best method to do it?

My method isn’t much better (maybe an expert can clarify us both) but
you can shave off a little work, and enforce a little more structure, by
using the date object instead of the time object:

Date.new(params[“start_date”][:year].to_i,
params[“start_date”][:month].to_i, params[“start_date”][:day].to_i)

Depending on what you are doing, there may be an easier way. If you just
want to update a table in your database, then you should use date_select
instead of select_date. The date_select helper will hand the data off to
ActiveRecord in a format that AR can handle for you.

<%= date_select ‘controller’ ‘method’ %>

This will bring the data back parsed out as follows:

{“date(1i)”=>“2006”, “date(2i)”=>“9”, “date(3i)”=>“8”}

… and you can call object.update_attributes params[:object] to have
ActiveRecord do all the work to make it a date and get it into the
database.

If, instead, you need to do something with that value besides putting it
into a model, then the way you or the other respondent are doing it is
pretty much it.

You could dig into ActiveRecord to find the code that translates this,
also.

c.

Chris wrote:

If i have a select_date in my view like this :

select_date @end_date,:prefix=>“end_date”

Then I parse it as follows in the controller:

@end_date =
Time.parse(params[“start_date”][:day]+"-"+params[“start_date”][:month]+"-"+params[“start_date”][:year]).to_date

This somehow doesn’t seem right. What is the best method to do it?

Thanks
Chris

On 9/8/06, Tom L. [email protected] wrote:

Date.new(params[“start_date”][:year].to_i,
params[“start_date”][:month].to_i, params[“start_date”][:day].to_i)

or

Date.new(params[“start_date”].values_at(%w[year month
day]).map(&:to_i))

I haven’t tried it though.


Kent