Noob Q: How to make list of cat's from selected items?

I have a table of items linked to a table of categories much like the
cookbook demo.

I’m currently aking a search function for the list, with pagination.

Would like to include on the page a list of categories, with #'s of
each, for the displayed items.

I know I could iterate over the list using:

@item.each do

and some code to make a list of item.category.title’s with incriments
for each occurance.

But I’m thinking there has to be a more eligant Ruby way of doing this,
that I just can’t see right now.

Hints, tips and pointers to simular code most welcome.

Lance. F. Squire

On 8/23/06, Lance S. [email protected] wrote:

@item.each do

and some code to make a list of item.category.title’s with incriments
for each occurance.

But I’m thinking there has to be a more eligant Ruby way of doing this,
that I just can’t see right now.

Hints, tips and pointers to simular code most welcome.

For a given category, you should be able to get the number of
associated Items with something like the following:

Item.count([“category_id = ?”, id])

That’s going to trigger a SQL query every time it’s called, so if you
have a lot of categories then you might want to use custom SQL to
fetch all the counts at once.

– James

James L. wrote:

Item.count([“category_id = ?”, id])

That’s going to trigger a SQL query every time it’s called, so if you
have a lot of categories then you might want to use custom SQL to
fetch all the counts at once.

– James

Ya, I was thinking I could count the ones already collected in the @item
list, rather than re-quering the database.

Woulden’t the above just give me the total number of items of that
category in the database? Rather than in my pre selected list?

Lance