Hello, I have been following the STI example in AWDWR and have a question about presenting the output. I want to output a list of Person objects that are grouped by their type. I have an array of all the Person objects and I would like the output to be as follows. Employee x3 <ul> <li>Employee 1</li> <li>Employee 2</li> <li>Employee 3</li> </ul> Manager x2 <ul> <li>Manager 1</li> <li>Manager 2</li> </ul> ... etc What is the best method for building the output in the view? Should I use acts_as_list or tree? You''ll notice I also want to have the count for each type. Is there a built in way to do this or should I hack something together? Thank you, Ryan Glover -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-31 20:34 UTC
Re: Presenting a grouped list of objects from STI
On Dec 31, 2:58 pm, Ryan Glover <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > > I have been following the STI example in AWDWR and have a question about > presenting the output. > > I want to output a list of Person objects that are grouped by their > type. > > I have an array of all the Person objects and I would like the output to > be as follows. > > Employee x3 > <ul> > <li>Employee 1</li> > <li>Employee 2</li> > <li>Employee 3</li> > </ul> > Manager x2 > <ul> > <li>Manager 1</li> > <li>Manager 2</li> > </ul> > ... etc > > What is the best method for building the output in the view? Should I > use acts_as_list or tree? You''ll notice I also want to have the count > for each type. Is there a built in way to do this or should I hack > something together?I would just pull each group out separately in the controller: @employees = Employee.find(:all) @managers = Manager.find(:all) It''s then simple to count and list them out in the view. Best, -r> > Thank you, > Ryan Glover > > -- > Posted viahttp://www.ruby-forum.com/.-- Ryan Raaum http://raaum.org http://rails.raaum.org -- Rails docs http://locomotive.raaum.org -- Self contained Rails for Mac OS X --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Glover wrote:> What is the best method for building the output in the view? Should I > use acts_as_list or tree? You''ll notice I also want to have the count > for each type. Is there a built in way to do this or should I hack > something together?I would use Enumerable#group_by @people = Person.find(:all) @people_by_type = @people.group_by(&:class) Number of managers: @people_by_type[Manager].size Number of employees: @people_by_type[Employee].size etc. To loop through: <% for klass in @people_by_type.keys %> <% for person in @people_by_type[klass] %> ..... Dan Manges --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---