|
Reads Postgresql's interval data type and transforms it into Rails' `ActiveSupport::Duration`. [PostgreSQL Docs](https://www.postgresql.org/docs/9.4/static/datatype-datetime.html#DATATYPE-INTERVAL-INPUT)
|
|
Reads PostgreSQL's interval data type and transforms it into Rails' `ActiveSupport::Duration`. [PostgreSQL Docs](https://www.postgresql.org/docs/9.4/static/datatype-datetime.html#DATATYPE-INTERVAL-INPUT)
|
|
|
|
|
|
# How it works
|
|
# How it works
|
|
|
|
|
|
### Migration
|
|
### Migration
|
|
|
|
|
|
|
|
Just set the type of the column as `interval` when creating a table.
|
|
|
|
```ruby
|
|
|
|
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.
|
|
|
|
```ruby
|
|
|
|
add_column :courses, :duration, :interval
|
|
|
|
```
|
|
|
|
|
|
### Using it
|
|
### 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](http://api.rubyonrails.org/classes/ActiveSupport/Duration.html)
|
|
|
|
```ruby
|
|
|
|
# Shows when you'll be finishing the course
|
|
|
|
course.duration.from_now
|
|
|
|
``` |
|
|
|
\ No newline at end of file |