PostgreSQL
 sql >> Base de données >  >> RDS >> PostgreSQL

Utilisation de l'intervalle dans PostgreSQL avec Ruby on Rails

Vous étiez proche :

class CreateExamples < ActiveRecord::Migration
  def change
    create_table :examples do |t|
      t.column :duration_min, :interval
      t.column :duration_max, :interval
      t.timestamps
    end
  end
end

Exemple d'utilisation :

Example.create duration_min: '2 hours', duration_max: '2 days'
#=> #<Example id: 1, duration_min: "2 hours", duration_max: "2 days", created_at: "2013-12-02 14:20:36", updated_at: "2013-12-02 14:20:36">
Example.where(%[TIMESTAMP ? - TIMESTAMP ? BETWEEN "duration_min" AND "duration_max"], DateTime.now, 10.hours.ago)
#=> #<ActiveRecord::Relation [#<Example id: 1, duration_min: "02:00:00", duration_max: "2 days", created_at: "2013-12-02 14:20:36", updated_at: "2013-12-02 14:20:36">]>