# Use select\_date for my model?

**URL:** <https://www.ruby-forum.com/t/use-select-date-for-my-model/55518>\
**Category:** Rails\
**Created:** [March 21, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/use-select-date-for-my-model/55518 "2006-03-21T20:07:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 21, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/use-select-date-for-my-model/55518/1 "2006-03-21T20:07:59Z")

</div>

Hi all

I have a model “party” that has a time field “starts\_at”. I have created  
a form to add new instances of this model.

\<%= select\_date Date.today, :prefix =\> ‘party’ %\>

Sadly I get the error:

NoMethodError in Parties#add  
undefined method `month=’ for #Party:0x256a600

How can I fix this problem? I remember that I saw in scaffolds that date  
selectors have something like party[year(1n)] or so as their names…  
How can I achieve this using the select\_date helper?

Thanks a lot.  
Greetings, Joshua

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 21, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/use-select-date-for-my-model/55518/2 "2006-03-21T21:56:33Z")

</div>

Joshua M. wrote:

> I have a model “party” that has a time field “starts\_at”. I have created  
> a form to add new instances of this model.
> 
> \<%= select\_date Date.today, :prefix =\> ‘party’ %\>
> 
> Sadly I get the error:
> 
> NoMethodError in Parties#add  
> undefined method `month=’ for #Party:0x256a600

Use date\_select instead:

View:

```
<%= date_select :party, :starts_at %>

```

Controller:

```
@party = Party.new( :starts_at => Date.today )

```

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 21, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/use-select-date-for-my-model/55518/3 "2006-03-21T22:56:35Z")

</div>

> Use date\_select instead:
> 
> View:
> 
> \<%= date\_select :party, :starts\_at %\>
> 
> Controller:
> 
> @party = Party.new( :starts\_at =\> Date.today )

Thanks a lot! But isn’t there also a time\_select? I tried it with

\<%= datetime\_select :party, :starts\_at, :discard\_day =\> true,  
:discard\_month =\> true, :discard\_year =\> true %\>

but sadly the year is not hidden… :-/

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 22, 2006, 7:08am UTC](https://www.ruby-forum.com/t/use-select-date-for-my-model/55518/4 "2006-03-22T07:08:37Z")

</div>

> If you’re only storing a time of day, why don’t you have  
> start\_hour and start\_minute DB fields, and use select\_hour  
> and select\_minute helpers?

I only want to split the datetime selection for layouting purposes…  
first the user can select the date, and then he can select the time. But  
maybe I’ll have to put it in the same place under these conditions.

Thanks anyway. 🙂

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 22, 2006, 9:17am UTC](https://www.ruby-forum.com/t/use-select-date-for-my-model/55518/5 "2006-03-22T09:17:13Z")

</div>

Joshua M. wrote:

> I only want to split the datetime selection for layouting purposes…  
> first the user can select the date, and then he can select the time. But  
> maybe I’ll have to put it in the same place under these conditions.

If you want to split the date and time you’ll have to use

View:

On: \<%= select\_date @party.starts\_at, :prefix =\> ‘party[starts\_at]’ %\>  
At: \<%= select\_hour @party.starts\_at, :field\_name =\>  
‘party[starts\_at][hour]’ %\>  
\<%= select\_minute @party.starts\_at, :field\_name =\>  
‘party[starts\_at][minute]’ %\>

Controller:

@party = Party.new( params[:party] )

Party model:

def starts\_at= (d)  
self[:starts\_at] =  
Time.local(d[:year],d[:month],d[:day],d[:hour],d[:minute])  
end

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 22, 2006, 3:34am UTC](https://www.ruby-forum.com/t/use-select-date-for-my-model/55518/6 "2006-03-22T03:34:39Z")

</div>

Joshua M. wrote:

> Thanks a lot! But isn’t there also a time\_select? I tried it with
> 
> \<%= datetime\_select :party, :starts\_at, :discard\_day =\> true,  
> :discard\_month =\> true, :discard\_year =\> true %\>
> 
> but sadly the year is not hidden… :-/

Yes, datetime\_select does not currently use the discard\_year  
option.

If the user is only able to select the time of day, how  
is the date of the party determined? Is it always today?

If you’re only storing a time of day, why don’t you have  
start\_hour and start\_minute DB fields, and use select\_hour  
and select\_minute helpers?

–  
We develop, watch us RoR, in numbers too big to ignore.
