Skip to main content

Benjamin Oakes

Photo of Ben Oakes

Hi, I'm Ben Oakes and this is my geek blog. Currently, I'm a Software Developer at Hedgeye. Previously, I was a Research Assistant in the Early Social Cognition Lab at Yale University and a student at the University of Iowa. I also organize NewHaven.rb. I do development with Ruby, JavaScript, SQL, HTML, and CSS. I have an amazing fiancée named Danielle Smith.

Filtering for the Rails category. Clear

Time in :conditions

by Ben

It took me quite a while to figure out that Time objects aren’t treated exactly as you might expect when using :conditions in a find or named_scope. From what I can tell, without extra help, Time objects are just treated as dates. This is fine if you’re checking conditions like I was outside the same day, but not if they are in the same day. This came up in writing a unit test for a model. My tests for the named scopes weren’t testing within the same day (i.e. only for values like 2.days.ago), so they passed, but other tests that expected to be able to set the open and close dates to a time within the same day would fail (confusingly).

So, it was incorrect to use the following, because Rails was treating them like a :date instead of a :datetime.

named_scope :active, lambda { now = Time.now; {:conditions => ['? > open_date and ? < close_date', now, now] } }

Instead, the following was used to make sure the time parts are included:

named_scope :active, lambda { now = Time.now; {:conditions => ['? > open_date and ? < close_date', now.to_s(:sql), now.to_s(:sql)] } }

Count vs length vs size in Rails

by Ben

I knew I had found a blog post about the differences at one point — I always get confused about it as well.

Loading environment fails with an outdated version of RubyGems

by Ben

My recent ticket for Rails 2.1 has gotten a fix that has been committed.