Ruby on Rails: Quick tip for Find :conditions

January 11th, 2008 I stumbled across a sneaky tip for conditions when using find. When you might normally be doing this:

1
2
Account.find(:all, :conditions => ['name LIKE ? AND updated_at < ?', 
"aname", 3.days.ago])
You can just do this instead:
1
2
Account.find(:all, :conditions => ['name LIKE :name AND updated_at < 
:date', {:name => "aname", :date => 3.days.ago}])

This isn't the best example of using this symbol placeholder method, but imagine using it in a situation where you're repeating the same search param a few times. I know I've had a few times that my conditions has been something like
1
2
['created_at > ? AND updated_at < ? 
AND invoiced_at > ?'], Time.now, Time.now, Time.now].
Using the hash at the end you could just pass this in as :time => Time.now and reuse :time in multiple places in your conditions hash. Neat.

Update: See ActiveRecord::Base on Noobkit for more info