Search code examples
ruby-on-railsrubyruby-on-rails-2

Issue counting using conditional datetime


I'm trying to count all my policies that are'n expired >= that today

  date_end >= TODAY

Here is my table

|policies|
  |id|  |num|    |date_ini|  |date_end|
   1    12484    2013-04-01   2014-05-01
   2    41511    2012-04-01   2013-05-01
   3    14441    2013-05-01   2014-06-01

There are 2 values that aren't expired

Here is my controller

 @policies =Policy.count(:conditions =>['date_end >= Date.now'])

I tried

 @policies =Policy.count(:conditions =>['date_end >= Date.today'])

And also

 @policies =Policy.count(:conditions =>['date_end >= curtime()'])

Please somebody can help me?

I will really appreciate help


Solution

  • It's not working because Date.today is inside a String, so it isn't being evaluated as an actual Date. You probably also want to use Date.current instead, to take the configured time zone into account.

    Try this instead:

    @policies = Policy.count( :conditions => ['date_end >= ?', Date.current] )