Skip to content

Instantly share code, notes, and snippets.

@pat
Created January 17, 2009 12:34
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 pat/48328 to your computer and use it in GitHub Desktop.
Save pat/48328 to your computer and use it in GitHub Desktop.
# Current Facets Support:
#
# Single-model searches only
#
class Person < ActiveRecord::Base
define_index do
indexes state, :facet => true
has year, :facet => true
facet city
end
end
# Use same arguments as #search method.
Person.facets "blah" #=>
# {
# :state => {
# "Victoria" => 10,
# "New South Wales" => 5,
# "Queensland" => 4,
# "Tasmania" => 3,
# "Northern Territory" => 3
# },
# :year => {
# 2006 => 5,
# 2007 => 8,
# 2008 => 10,
# 2009 => 2
# },
# :city => {
# "Melbourne" => 8,
# "Bendigo" => 2,
# "Sydney" => 4,
# "Gosford" => 1,
# "Brisbane" => 4,
# "Launceston" => 1,
# "Hobart" => 2,
# "Darwin" => 2,
# "Alice Springs" => 1
# }
# }
@facets = Person.facets("blah")
@facets.for(:state => "Victoria") #=>
# [
# <#Person ..>,
# <#Person ..>,
# <#Person ..>,
# <#Person ..>,
# ...
# ]
# You can drill down by multiple facets at once, too:
@facets.for(:state => "Victoria", :city => "Melbourne")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment