Using like in multiple conditions

You can modify the value of params[:order] in your controller/action.

def results
  params[:order][:name] += '%'
  yet_more = Order.find(:all, :conditions => ["name LIKE :name AND
pay_type = :pay_type", params[:order]])
end

This will add the % operator to the end of the name string being passed
from the submitted form.

-Jared

If the params[:order][:name] parameter is left blank on your search
form, you could do something like this in your controller:

unless params[:order][:name].blank?
  params[:order][:name] += '%'
else
  params[:order][:name] = '%'
end
yet_more = Order.find(:all, :conditions => ["name LIKE :name AND
pay_type = :pay_type", params[:order]])

That way, if the field is left blank on the search form, the
Order.find() method will include all names, and just match on the
pay_type. I don't know if I'd call it more elegant, but it's a bit
more explicit in what the code is doing.

-Jared

Consider wrapping this up in a custom finder on Order… that way you can

  1. put conditions in the model and do evaluations on the fields there… keeping controller nice and clean.
  2. unit test this a lot easier