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

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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





can you show the actual SQL fired?
– emaillenin
3 mins ago









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.

UkpjhSET6BSALia,CAqGIs dYyBDbtcuZzioM0W4T403LTtSkQxa hv Al,Y
ng9 PPG1kjuE6gQUrqfd9,YyJZtWl5HgACmoj,1Ndg9WWl3hkBSjUUWyTFHJfHgPA1M,2vpNeQVJBBICx,QwtH,Q,yp

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham