randomguy
randomguy

Reputation: 12242

How to store length of time in a model with Rails?

I can think of two solutions:

1) Store hours, minutes, seconds, etc. in separate columns in the database

2) Convert and store the number of seconds

Am I missing some other obvious solution? How do you people do this?

Upvotes: 2

Views: 2508

Answers (2)

edk750
edk750

Reputation: 1110

I vote for a single column to keep track of durations. That keeps the duration normalized, whereas you'll need to do almost as much work or more to normalize multi-column durations.

Rails (activesupport) also gives you wonderful methods to work with time. For example, if your duration is in seconds, you can easily add the duration in seconds to a point in time and get an end time:

end_time = Time.now + duration_in_secs

It is a bit more work using virtual attributes to separate it out, but in my experience, it's not that more work. perhaps there is a plugin or gem that simplifies it.

Upvotes: 3

Ant
Ant

Reputation: 3887

I prefer to have separate columns in the database if I'm receiving the input via 3 fields on the form - it keeps things much cleaner and simpler (and after all, nowadays a few extra columns in a table isn't much to worry about). Then all you need is a nice method to output the stuff nicely.

Upvotes: 1

Related Questions