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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.