I'm pretty sure you want to use COALESCE inside the database:
customer = Customer.where(:siteId => params[:siteId])
.where('coalesce(customercode, temporarycode) = ?', params[:id])
.first
The SQL COALESCE function returns the first of its arguments that isn't NULL. For example:
coalesce(column1, column2)
gives you column1
if column1
isn't NULL and column2
if column1
is NULL.
If you're using Rails2 then something like this should work:
Customer.find(:first, :conditions => [
'siteid = ? and coalesce(customercode, temporarycode) = ?',
params[:siteId], params[:id]
])