ruby-on-railsrubyruby-on-rails-3timefixnum

ruby on rails 3.0 TimeWithZone add hours can't be coerced into Fixnum


I'm getting a really strange error in my code that I'm hoping someone can help me out with. I have a model which has a created_at column and a duration column I have created. The duration is simply an integer which is assumed to be a number of hours. I am then trying to compare the current time to the created_at time + the duration to see if the row has expired. My code looks like this:

@expire_time = purchase.duration.hours + purchase.created_at
if(@expire_time > Time.current)
  #do some stuff if the current time isn't greater than the expiration time
end

When I run that first line in the console it works fine, but when I save my code and call it through the route its part of, I get the following error at that line.

ActiveSupport::TimeWithZone can't be coerced into Fixnum

Does anyone have any thoughts on what might be the issue/how I go about making this comparison properly?


Solution

  • I found the problem...

    this does not work: purchase.duration.hours + purchase.created_at

    but this does: purchase.created_at + purchase.duration.hours