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

**URL:** <https://www.ruby-forum.com/t/which-is-the-most-ruby-style-way-to-do-this-rubies-only/68012>\
**Category:** Rails\
**Created:** [September 8, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/which-is-the-most-ruby-style-way-to-do-this-rubies-only/68012 "2006-09-08T14:43:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 8, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/which-is-the-most-ruby-style-way-to-do-this-rubies-only/68012/1 "2006-09-08T14:43:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 8, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/which-is-the-most-ruby-style-way-to-do-this-rubies-only/68012/2 "2006-09-08T19:44:50Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 8, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/which-is-the-most-ruby-style-way-to-do-this-rubies-only/68012/3 "2006-09-08T20:54:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 8, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/which-is-the-most-ruby-style-way-to-do-this-rubies-only/68012/4 "2006-09-08T21:42:58Z")

</div>

On 9/8/06, Tom L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

> **[DATANOISE.COM](https://www.datanoise.com/)**
>
> DATANOISE.COM - Contact us for any business inquiries
