ValeMarz
ValeMarz

Reputation: 101

How to save measured time in Rails 5?

I have a js timer and I need to save the time in format: hour minute second. Like you know Timer.new(hour, minute, sec) (similar). How can i save an interval time in rails db? I just need to know how to save it, i know ho to handle ajax/controller stuff.

Upvotes: 0

Views: 53

Answers (1)

slowjack2k
slowjack2k

Reputation: 2586

You have multiple options. You can create two timestamp columns within your database like this:

create_table "your_table", force: true do |t|
  # ...
  t.timestamp "started_at"
  t.timestamp "ended_at"
end

Or you can create one timestamp and one integer column:

create_table "your_table", force: true do |t|
  # ...
  t.timestamp "started_at"
  t.integer  "elapsed_time"
 end

Or you store only elapsed time:

create_table "your_table", force: true do |t|
  # ...
  t.integer  "elapsed_time"
end

started_at and ended_at should be a timestamp or datetime, in my point of view. I would not only store time, because it makes calculations much more easier. For instance when started_at is today and ended_at tomorrow.

Keep in mind that it is database dependent if Rails stores time values with a precision in seconds or microseconds.

Upvotes: 2

Related Questions