Lance Squire
2006-Aug-23 18:58 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
James Ludlow
2006-Aug-23 19:47 UTC
[Rails] Re: Noob Q: How to make list of cat''s from selected items?
On 8/23/06, Lance Squire <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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.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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Lance Squire
2006-Aug-23 20:12 UTC
[Rails] Re: Noob Q: How to make list of cat''s from selected items?
James Ludlow 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. > > -- JamesYa, 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---