I am trying to make a list of categories returned by a search of the
items table. Including # of times that category showed up in the list.
The items and categories tables are linked like the recipe tutorial.
That is:
<% @items.each do |item| -%>
<%= items.category.name %>
<% end -%>
will list all the category names.
I’ve found code that will enumerate how many times something shows up
in an array, But have no idea how to apply it to the list of items.
Here’s the link to the code: (Doh lost link is browser crash)
This is the heart of the code from another site:
inject(Hash.new(0)) {|hash, element| hash[element] += 1; hash}
Unfortunatly, I have no idea how to get the list of categories from the
@items and feed them to/through this code to get the result I want. (Or
even something I can work with)
What I would idealy like to end up with is a @categories containing
name, id and count so I could use code like:
<% @categories.each do |cat| %>
<%= cat.name%> <% if cat.count -%> <%= cat.count %> <% end -%>
<% end -%>in the view.
Thanks
Lance F. Squire