I'm attempting to create a search method on one of my Active Record models.
I want users to be able to search for a record in the database.
I know I can do something like this: Model.where('field like ?', "%#{query}%')
But it's not just one field, it's two fields (first name and last name). But I don't want to search them separately, I want to concatenate first name and last name and search where the concatenated field is like the search query.
Can this be done with Active Record similar to how SQL fields can be concatenated? What's the best solution?
Model.where('first_name LIKE ? OR last_name LIKE ?', "%#{query}%", "%#{query}%")
. If you don't like like this use MySQL's full text search capabilities: dev.mysql.com/doc/refman/5.0/en/fulltext-search.html