Reputation: 6317
Users can currently apply filters to a database query by setting checkboxes, e.g.
Colour
[] Red
[] Green
[] Blue
Shape
[] Round
[] Square
Size
[] Small
[] Medium
[] Large
Clicking on a checkbox triggers an AJAX request which appends the where
condition to the query, e.g.
@mymodel = MyModel.scoped
@mymodel = @mymodel.where(:colour => params[:colour]) if params[:colour]
...
What I would like to do now is add a count after each checkbox to indicate how many results would be displayed if a given checkbox is selected. e.g.
Colour
[] Red (148)
[] Green (121)
[] Blue (136)
...
Upon clicking a checkbox, the counts next to all checkboxes should be refreshed, and options pertaining to a zero result will not be displayed. This needs to work across all filter categories, so for example, if there is nothing small and red in the database, the 'Small' option should disappear when 'Red' is selected, and re-appear when it is deselected.
What is the best way to achieve this type of predictive filtering?
Many thanks.
Update
More specifically, my question is: What is the best way to implement faceted navigation in Rails?
(Thanks @Pasta for the identifying the term for this)
Upvotes: 3
Views: 2190
Reputation: 1364
I wrote a gem for ActiveRecord based filtering and facet calculation, that does not depend on external services/deamons: https://github.com/fortytools/forty_facets
Upvotes: 0
Reputation: 15492
There are quite a few open-source search engines that will give you faceted search. We use Thinking Sphinx and have been very happy with it. It looks like they have preliminary support for Rails 3, but it might be worth a shot. The facet syntax is fairly straightforward:
Example:
# This will return any search results for 'pancakes'
Article.search 'pancakes'
# This will return any facets for 'pancakes'
Article.facets 'pancakes'
You might also want to look at Solr and Ferret -- both of which I believe support faceted search.
Upvotes: 6
Reputation: 9577
although I can't answer fully, I suggest you look into updating this "form" or partial through ajax. Ryan had a railscast 43 will give you a few ideas on how it can work, but then you can research how to do all the fancy stuff with jquery and rails3.
Upvotes: 0