Skip to content

Instantly share code, notes, and snippets.

@jpemberthy
Created May 18, 2011 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpemberthy/8257e7c4e512aa8a45df to your computer and use it in GitHub Desktop.
Save jpemberthy/8257e7c4e512aa8a45df to your computer and use it in GitHub Desktop.
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment