secret
Last active

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# Let's say I have the following user.
 
user = User.create(:company_name => "The cheappo restaurant")
 
# And the following User finder that works with `starting_with`.
 
def self.find_all_by_indexed_company_name(term)
Sunspot.search(self) do |q|
q.text_fields { with(:company_name).starting_with(term) }
end.results
end
 
# Now let's try some queries
 
User.find_all_by_indexed_company_name("the")
=> user
User.find_all_by_indexed_company_name("the ch")
=> user
 
# Now if I search for "estaurant"
User.find_all_by_indexed_company_name("estaurant")
=> []
 
# This is why I say `starting_with` partially works for me,
# I would like to have an indexed search using something like `LIKE`:
 
User.find(:all, :conditions => ["company_name LIKE ?", "%estaurant%"])
=> user

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.