# Selecting time

**URL:** <https://www.ruby-forum.com/t/selecting-time/74696>\
**Category:** Rails\
**Created:** [December 24, 2005, 12:33am UTC](https://www.ruby-forum.com/t/selecting-time/74696 "2005-12-24T00:33:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![froggie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9e5f3/32.png) [@froggie](https://www.ruby-forum.com/u/froggie)\
**Post date:** [December 24, 2005, 12:33am UTC](https://www.ruby-forum.com/t/selecting-time/74696/1 "2005-12-24T00:33:16Z")

</div>

I’m currently building a small app that’s going to be used to keep track  
of the amount of time spend on customers of the company I work for.  
This mainly consists of object with a begin time, an end time and a  
calculation of the difference between those timestamps (actual time  
spend on the job).

I made a small form with a datetime\_select helper for the begin and end  
time (the column type inside the DB is also dateselect). Because the  
actual time spend on the job can be less then the difference between  
begin and end I want this time to be selectable too. Because this  
attribute only holds hours and minutes I tried to use the select\_time  
helper. Unfortunately there seems to be a small bug in this helper cause  
the fieldname that’s being parsed is always date[“name you give to it  
using the :field\_name option”].  
I just tried the datetime\_select using the :discard\_year and -month  
options, but this helper always needs the year selection field (probably  
has something to do with the fact that Time.gm() requires at least the  
year argument). Although this works (the value inside the ‘time’ column  
is nicely being mapped to the ‘time’ field it just doesn’t look right  
with the year selection field being displayed.

The question is: is there an easy and elegant way to solve this problem  
or will I have to build my own time\_select helper using the hour and  
minute field?

---

<div class="post-metadata">

**Author:** ![froggie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9e5f3/32.png) [@froggie](https://www.ruby-forum.com/u/froggie)\
**Post date:** [December 24, 2005, 7:14am UTC](https://www.ruby-forum.com/t/selecting-time/74696/2 "2005-12-24T07:14:11Z")

</div>

Have you seen [http://www.formassembly.com/time-tracker/](http://www.formassembly.com/time-tracker/) ?

I think it is a nice solution to the problem you are trying to solve.  
Of  
course if you want it to be local or want to solve the problem for the  
purpose of learning, that is a different story.

---

<div class="post-metadata">

**Author:** ![froggie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9e5f3/32.png) [@froggie](https://www.ruby-forum.com/u/froggie)\
**Post date:** [December 25, 2005, 12:40pm UTC](https://www.ruby-forum.com/t/selecting-time/74696/3 "2005-12-25T12:40:50Z")

</div>

Eden B. wrote:

> Have you seen [http://www.formassembly.com/time-tracker/](http://www.formassembly.com/time-tracker/) ?

It looks like a nice standalone application, but I’m writing this app so  
it can be integrated in our intranet and ticketsystem. Besides that, I’m  
using this project to give myself a crash course RoR 🙂

Anybody else who is experiencing my ‘problem’?

---

<div class="post-metadata">

**Author:** ![froggie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9e5f3/32.png) [@froggie](https://www.ruby-forum.com/u/froggie)\
**Post date:** [December 25, 2005, 4:44pm UTC](https://www.ruby-forum.com/t/selecting-time/74696/4 "2005-12-25T16:44:56Z")

</div>

Arjen Roodselaar wrote:

> It looks like a nice standalone application, but I’m writing this app so  
> it can be integrated in our intranet and ticketsystem. Besides that, I’m  
> using this project to give myself a crash course RoR 🙂
> 
> Anybody else who is experiencing my ‘problem’?

Yes, I noticed the exact same thing using select\_time and the  
:field\_name option. I solved this by using the :prefix option to set the  
field name to something else (in my case ‘starttime’).  
My html source now shows and .

In the create function in my controller I do the following to get the  
correct time (starttime is a mysql ‘time’ column in a ‘users’ table):

@user.starttime = Time.gm(2005,1,1,@params[‘starttime’][‘hour’],  
@params[‘starttime’][‘minute’])

I’m a rails newbie, so I don’t know if this is the correct ‘rails way’  
to handle this, it worked for me but it seemed cumbersome. Is there any  
info anywhere on how I can make a select\_time helper myself to work just  
like the datetime\_select helper ?

While doing this, being new to rails (and only hacked together a couple  
of lines of code in ruby), I wondered why params[:starttime][:minute]  
seemed to be the same as @params[‘starttime’][‘minute’]. Is it the same  
? Which notation is preferred ?

Piet.

---

<div class="post-metadata">

**Author:** ![froggie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9e5f3/32.png) [@froggie](https://www.ruby-forum.com/u/froggie)\
**Post date:** [December 25, 2005, 8:56pm UTC](https://www.ruby-forum.com/t/selecting-time/74696/5 "2005-12-25T20:56:35Z")

</div>

Piet, thanks for your input. I was already thinking about doing it the  
same way, but I’m still having problems with my ‘edit’ action (the  
values are not being put back into the form). If you happened to solve  
this problem, sharing your code would be appreciated!

---

<div class="post-metadata">

**Author:** ![froggie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9e5f3/32.png) [@froggie](https://www.ruby-forum.com/u/froggie)\
**Post date:** [December 26, 2005, 1:22pm UTC](https://www.ruby-forum.com/t/selecting-time/74696/6 "2005-12-26T13:22:05Z")

</div>

Piet,

Sorry, you’re right you posted your code. I think I was sleeping  
yesterday. I managed to hack up the original datetime\_select (wasn’t  
that hard after all) to allow just time selection. It’s still a bit  
messy, so I want to clean up first before posting. I’ll probably submit  
it as a patch to the developers so it can be included in the next  
release.

---

<div class="post-metadata">

**Author:** ![froggie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9e5f3/32.png) [@froggie](https://www.ruby-forum.com/u/froggie)\
**Post date:** [December 25, 2005, 10:38pm UTC](https://www.ruby-forum.com/t/selecting-time/74696/7 "2005-12-25T22:38:40Z")

</div>

Arjen Roodselaar wrote:

> Piet, thanks for your input. I was already thinking about doing it the  
> same way, but I’m still having problems with my ‘edit’ action (the  
> values are not being put back into the form). If you happened to solve  
> this problem, sharing your code would be appreciated!

Well, I just did… here’s what I put in my form (or should I say  
view?):

Start Time  
\<%= select\_time (@user.starttime, :prefix =\> 'starttime') %\>

In my controller:

def update  
@user = User.find(params[:id])

```
@user.starttime = Time.gm(2005,1,1,
           @params['starttime']['hour'],
           @params['starttime']['minute'])

if @user.update_attributes(params[:user])

```

Hope this helps. Since these are my first rails steps I really don’t  
know what I’m doing and can’t give you any decent explanation or even  
name things the way they’re called. I hope you realize I just handed  
over almost 100% of the rails code I made thus far 😉

I just wished someone would be able to point at how to make a decent  
select\_time helper function along the lines of how datetime\_select  
works. It suprises me there’s no standard ActiveRecord way to handle  
‘time’ columns. I guess I’ll have to dig into the rails bowels myself…  
(or maybe I’m not seeing ‘it’ yet?)

Piet.
