Skip to content

Instantly share code, notes, and snippets.

@toddsampson
Created November 13, 2013 21:08
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 toddsampson/7456440 to your computer and use it in GitHub Desktop.
Save toddsampson/7456440 to your computer and use it in GitHub Desktop.
Default scope with Sunspot blog post: 2010/01/15/default-scope-with-sunspot/
class Item
# simple default scope to add an order_by to all queries
def self.default_search_scope(&additional)
search = Sunspot.new_search(Item)
search.build(&additional)
search.build do
order_by :created_at, :desc
end
search.execute
search.results
end
# pardon the contrived example
def self.with_keywords(user, query)
default_search_scope do
keywords query
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment