I am writing a rails 3 application, and I have a database table of Users. The users have first_name and last_name columns. On my page that lists the users, I want to have a single text search box that allows the user to filter the list. They need to be able to type into the search box one of the following:
– A first name of a user
– A last name of a user
– A full name of a user (eg. “Smith, John”)
I do not know how to write the where clause that will return these results. The closest I have gotten is below, however the concatenated fields part does not work.
where('(first_name LIKE :search) or (last_name LIKE :search) or ("#{last_name}, #{first_name}" LIKE :search)', :search => "%#{search}%")
I am also interested to know if there is a better way to handle my OR statements, as this clause is getting quite clunky.
Thanks for your help!
In case it helps anyone else, this is the solution I went with:
Thanks to Ryan Bates of Railscasts for this solution.