Deepak Kabbur
Deepak Kabbur

Reputation: 802

Saves nil for daterange for same date Rails 4.2.7

I am using Rails : 4.2.7 and Postgress : 9.3

Migration

def change
  create_table :category do |t|
    t.float :price
    t.daterange :date_range
    t.timestamps null: false
  end
end

When I am trying to save date_range with same date

from = Date.today.beginning_of_day
to = Date.today.end_of_day 

Category.create(date_range: from..to)

saves nil for date_range.

Its work perfectly if dates are different like

from = Date.yesterday.beginning_of_day
to = Date.today.end_of_day

One solution is create separate columns from and to.

Upvotes: 0

Views: 65

Answers (1)

edruid
edruid

Reputation: 703

Dateranges only operates on dates, that is your beginning of day and end of day will both be truncated to today which creates an empty range (which apparently is translated as nil)

What you most likely mean is to use Date.today (inclusive) to Date.tomorrow (exclusive)

If you actually want to store the times you should use a tsrange.

Upvotes: 1

Related Questions