Removing spaces before querying database in Active Record works in Rails C but not controller

Multi tool use


Removing spaces before querying database in Active Record works in Rails C but not controller
I am setting up an AJAX response to auto-populate the data list on a search bar as the user types. So far it works perfectly, except that if the user adds additional spaces in the word, it won't find the result. I am trying to remove the spaces when querying the database for similar results by adding some additional SQL into my .where arguments. It still works for the previous case of when there are no extra spaces, but when the user enters extra spaces it does not work.
The Listing.where("replace(city,' ','') like replace(?, ' ', '')", query)
query works perfectly when I simply run it in Rails console for testing, so I don't understand why it's not working from the controller method.
Listing.where("replace(city,' ','') like replace(?, ' ', '')", query)
Here is my controller method:
def search
query = "%#{params[:query]}%"
# Paginate full list of matching listings for rendering search results to html
@listings = Listing.where("city like ?", query).page
# Find unique list of matching cities for ajax requests to autocomplete search bar
@cities = Listing.where("replace(city,' ','') like replace(?, ' ', '')", query).pluck(:city).uniq
respond_to do |format|
format.html { @listings.page }
format.js { render :json => @cities }
end
end
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
can you show the actual SQL fired?
– emaillenin
3 mins ago