-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
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"
endOr when you are adding or column, just use :interval as type.
add_column :courses, :duration, :intervalThe column is automatically identified and the value is turned into ActiveSupport::Duration. So, any of the methods available on it can be used directly from your field. RubyOnRails Doc
# Shows when you'll be finishing the course
course.duration.from_nowThe value can be set in different manners:
course.duration = 1.year # A new instance of ActiveSupport::Duration
course.duration = [1, 2, 3, 4, 5, 6] # A list of values for the given order Y-M-D H:M:S
course.duration = [1, 0, 0] # The list uses the right-most available positions, so this is H:M:S
course.duration = {days: 1, hours: 6} # You can use complex hash notation
course.duration = 'P1Y2M3DT4H5M6S' # It accepts ISO 8601 format, so form fields can cast to this type
course.duration = 259200
course.duration.parts.to_h == {days: 3} # It also converts any set value to the correct separated durationCan't find what you're looking for? Add an issue to the issue tracker.