Skip to content

Interval

Carlos edited this page Apr 12, 2017 · 7 revisions

Reads PostgreSQL's interval data type and transforms it into Rails' ActiveSupport::Duration. PostgreSQL Docs

How it works

Migration

Just set the type of the column as interval when creating a table.

create_table "courses" do |t|
  t.string   "title",    null: false
  t.interval "duration"
end

Or when you are adding or column, just use :interval as type.

add_column :courses, :duration, :interval

Using it

The column is automatically identified and the value is turn into ActiveSupport::Duration. So any of the methods available on it can be used direct from your field. RubyOnRails Doc

# Shows when you'll be finishing the course
course.duration.from_now
Clone this wiki locally