I have a users table with name and surname and a search form with only a full_name field. How can I get it working?
I tried with this simple code:
where_clause = "(name + ' ' + surname) LIKE ?"
# page is a will_paginate method
@users = User.where([where_clause, "%#{params[:full_name]}%"]).page params[:page]
The above code produces:
SELECT "users".* FROM "users" WHERE ((name + ' ' + surname) LIKE '%test%') LIMIT 30 OFFSET 0
… which gives no results! Why?
I think the problem relies in the concatenation, but I can’t see any alternatives. In fact I can’t modify the select clause (i.e. “*, (name + ‘ ‘ + surname) AS full_name”) because the page method doesn’t work with active record find, and will_paginate methods (i.e. page or paginate) seems not to support conditions any more.
Try using: