The easiest way to query AR is to simply use an id, like this:
User.find(10)
This uses this sql, nicely referenced by the table name:
SELECT * FROM users WHERE (users.`id` = 10)
The downside to this is that a plain #find call will throw an exception if no record exists, and that’s not good (unless caught, the client will see a 500 at that point).
We’ve been getting around this by using find calls like this:
User.find( :all, :conditions => ['id = ?', 10] )
Which generates this sql:
SELECT * FROM users WHERE (id = 10)
This way is good because it will return nil if no record is found, instead of throwing an exception. The subtle downside is that the string in the conditions parameter is copied directly into the sql string, because AR doesn’t know how to do anything with it.
If instead you use a hash for the conditions parameter, like this:
User.find( :all, :conditions => { :id => 10 } )
AR knows that you’re referencing a column from that model’s table, and gives you the safe sql with no bad exception aftertaste:
SELECT * FROM users WHERE (users.`id` = 10)
As an added bonus, because AR is allowed to do it’s magic, if you decide to pass it an array instead of a single integer, it can do the right thing without you having to change your #find code:
User.find( :all, :conditions => { :id => [10,12] } )
Which automatically comes out as:
SELECT * FROM users WHERE (users.`id` IN (10,12))
Summary:
Don’t do this:
User.find(10)or this: User.find( :all, :conditions => ['id = ?', 10] ).
Do this:
User.find( :all, :conditions => { :id => 10 } )