1

I'm getting the error while comparing two dates in ruby on rails, I'm using PostgreSQL as my database.

exp_date = ps.expiry_date
if Date.today < Date.parse(exp_date)
  Good
else
  Expired

exp_date variable contains 2014-07-31 value from database.

But while I'm running this code it's giving this error "can't convert Date into String" in the 2nd line of my code snippet. But when using directly the date as 2014-07-31, it's working. Please help me to find out where I'm wrong.

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317

1 Answers1

0

The only probable reason of getting the error, when Date::parse will get an Date object as argument. Like below :-

Date.parse(Date.today)
# TypeError: no implicit conversion of Date into String

I am quite sure, ps.expiry_date is giving you Date object, not a String. As your DB already is giving you the data as Date object, you don't need to parse it furthur as Date. Simply write as :

Date.today < exp_date
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317