Panasonic Youth

Quick: Find the Bug or Gotcha with named_scope

Think fast! Where’s the bug?

  named_scope :active, :conditions => ["activated_at < = ?", DateTime.now.utc.to_s(:db)]

Looks fine, right? Maybe you’ve hit this already, and you see it immediately.

The symptoms are that the DateTime.now always seems to be a bit off - maybe you just restarted your server and its a only a few minutes off.

The bug is that DateTime.now gets evaluated at the time the class is loaded, not when the finder is run. What makes this easy to miss is that it will always work fine in tests and development, as everything is constantly getting reloaded there.

The fix, obvious once you’ve spent a combined time of over an hour trying to figure out what is going on:

  named_scope :active, lambda { { :conditions => ["activated_at <= ?", DateTime.now.utc.to_s(:db)] } }