Home » Ruby » comparison of Date with ActiveSupport::TimeWithZone failed

comparison of Date with ActiveSupport::TimeWithZone failed

Posted by: admin December 20, 2017 Leave a comment

Questions:

I have an age method on my Waiver model that looks like:

  def age(date = nil)

    if date.nil?
      date = Date.today
    end
    age = 0
    unless date_of_birth.nil?
      age = date.year - date_of_birth.year
      age -= 1 if date < date_of_birth + age.years #for days before birthday
    end
    return age
  end

I then have a spec that looks like:

it "calculates the proper age" do
 waiver = FactoryGirl.create(:waiver, date_of_birth: 12.years.ago)
 waiver.age.should == 12
end

When I run this spec I get comparison of Date with ActiveSupport::TimeWithZone failed. What am I doing wrong?

Failures:

  1) Waiver calculates the proper age
     Failure/Error: waiver.age.should == 12
     ArgumentError:
       comparison of Date with ActiveSupport::TimeWithZone failed
     # ./app/models/waiver.rb:132:in `<'
     # ./app/models/waiver.rb:132:in `age'
     # ./spec/models/waiver_spec.rb:23:in `block (2 levels) in <top (required)>'
Answers:

You are comparing an instance of Date with an instance of ActiveSupport::TimeWithZone in the expression date < date_of_birth + age.years; ActiveSupport::TimeWithZone is, according to the docs, a Time-like class that can represent a time in any time zone. You simply can’t compare Date and Time objects without performing some kind of conversion. Try Date.today < Time.now on a console; you’ll see a similar error.

Expressions like 12.years.ago and typical ActiveRecord timestamps are instances of ActiveSupport::TimeWithZone. You are best off ensuring that you deal only with Time objects or Date objects, but not both in this method. To make your comparison date-to-date, the expression could instead be written as:

age -= 1 if date < (date_of_birth + age.years).to_date