Hi all, first time poster. I am currently learning RoR, and have run into a snag. I have a table called Games that has several fields, including title, desc, genre, system, etc. What I am looking to do in a view is create an output that is grouped by system. For example... Wii ... Wii Sports ... Metroid Prime 3 Xbox 360 ... Halo 3 My first thought was to use nested for loops, looping through each unique system, and then looping through each game that matches that system, and while I know how to do this in PHP (using procedural code), I am struggling with how to contain my iteration objects within the controller, and access them only in the view. Any thoughts? I hope I am explaining this well. Thanks, Martin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Welcome, Martin. The Rails API docs <http://api.rubyonrails.org> show that ActiveRecord::Base#find<http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001004>can take a :group option to do what you want. For example, Game.find(:all, :group => "system") Craig On 10/8/07, Martin Austin <mraustin82-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Hi all, first time poster. > > I am currently learning RoR, and have run into a snag. I have a table > called Games that has several fields, including title, desc, genre, > system, etc. What I am looking to do in a view is create an output > that is grouped by system. For example... > > Wii > ... Wii Sports > ... Metroid Prime 3 > Xbox 360 > ... Halo 3 > > My first thought was to use nested for loops, looping through each > unique system, and then looping through each game that matches that > system, and while I know how to do this in PHP (using procedural > code), I am struggling with how to contain my iteration objects within > the controller, and access them only in the view. > > Any thoughts? I hope I am explaining this well. > > Thanks, > > Martin > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sorry for the font size weirdness in my reply (or is it only showing for me?). On 10/8/07, Craig Demyanovich <cdemyanovich-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Welcome, Martin. > > The Rails API docs <http://api.rubyonrails.org> show that > ActiveRecord::Base#find<http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001004>can take a :group option to do what you want. For example, > > Game.find(:all, :group => "system") > > Craig > > On 10/8/07, Martin Austin <mraustin82-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Hi all, first time poster. > > > > I am currently learning RoR, and have run into a snag. I have a table > > called Games that has several fields, including title, desc, genre, > > system, etc. What I am looking to do in a view is create an output > > that is grouped by system. For example... > > > > Wii > > ... Wii Sports > > ... Metroid Prime 3 > > Xbox 360 > > ... Halo 3 > > > > My first thought was to use nested for loops, looping through each > > unique system, and then looping through each game that matches that > > system, and while I know how to do this in PHP (using procedural > > code), I am struggling with how to contain my iteration objects within > > the controller, and access them only in the view. > > > > Any thoughts? I hope I am explaining this well. > > > > Thanks, > > > > Martin > > > > > > --~--~---------~--~----~------------~-------~--~----~ > > 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 > > -~----------~----~----~----~-- ----~----~------~--~--- > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Em Seg 08 Out 2007, Craig Demyanovich escreveu:> Sorry for the font size weirdness in my reply (or is it only showing for > me?). >Relax... :-) We''ll forget about your top-post and HTML-post... :-) -- Davi Vidal -- E-mail: davividal-UiHwsRqXctc1RhZgQKG/ig@public.gmane.org MSN : davividal-uAjRD0nVeow@public.gmane.org GTalk : davividal-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org Skype : davi vidal ICQ : 138815296
jamal.hansen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-09 17:55 UTC
Re: Generating a Grouped Output
I think you are not looking for the SQL group by clause, but rather grouping on the html view. You can do your find to include the necessary joins from games to system (maybe vice versa?) and then render a partial that contains the system and a nested partial to list games. Not sure if this is the most efficient way, but it works and you get reusable partials. -Jamal On Oct 8, 12:48 pm, Martin Austin <mrausti...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, first time poster. > > I am currently learning RoR, and have run into a snag. I have a table > called Games that has several fields, including title, desc, genre, > system, etc. What I am looking to do in a view is create an output > that is grouped by system. For example... > > Wii > ... Wii Sports > ... Metroid Prime 3 > Xbox 360 > ... Halo 3 > > My first thought was to use nested for loops, looping through each > unique system, and then looping through each game that matches that > system, and while I know how to do this in PHP (using procedural > code), I am struggling with how to contain my iteration objects within > the controller, and access them only in the view. > > Any thoughts? I hope I am explaining this well. > > Thanks, > > Martin--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---