- Store as integers in your database (number of seconds, probably).
- Your entry form will depend on the exact use case. Dropdowns are painful; better to use small text fields for duration in hours + minutes + seconds.
- Simply run a
SUM
query over the duration column to produce a grand total. If you use integers, this is easy and fast.
Additionally:
- Use a helper to display the duration in your views. You can easily convert a duration as integer of seconds to
ActiveSupport::Duration
by using123.seconds
(replace123
with the integer from the database). Useinspect
on the resultingDuration
for nice formatting. (It is not perfect. You may want to write something yourself.) - In your model, you’ll probably want attribute readers and writers that return/take
ActiveSupport::Duration
objects, rather than integers. Simply defineduration=(new_duration)
andduration
, which internally callread_attribute
/write_attribute
with integer arguments.