简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM