ruby-on-rails – Mapear Substring Solr

Question:

I have the following question, I have the model city(name, uf, province) I'm trying to implement the sunspot search method, but I'm succeeding in part:

# modelCity.rb
searchable do
  text :name 
end 

# controller City
unless params[:search] 
  @cities = City.paginate(:page => params[:page], :order => "name ASC")
else
  @search = City.search do
    fulltext params[:search]
  end
  @cities = @search.results
end

city ​​database:

1 Medianeira PR 3
2 Cascavel   PR 4
3 Cascavemat PR 3

If I type for example "Cascavel" it brings up the right search, but if I type "Casca" I want it to bring up all the cities that have those words related to the name. Does anyone know how I do this?

Answer:

If you are using an SQL Database, you can use wildcards:

query = "CASCA"
City.where("name like ?", "%#{query}%")

* In this way, the framework already handles problems like SQL Injection

Scroll to Top