SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Grouping output in view - Nuby Question

    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

  2. #2
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would make a seperate table called systems
    Then in the games table have system_id instead of system

    Code ruby:
      #in game.rb
         belongs_to :system
      # in system.rb
         has_many :games
     
      # in your controller
        @systems = System.find(:all)
     
      # view
        <% @systems.each do |system| %>
            <h3><%= system.name %></h3>
            <ul>
              <% system.games.each do |game| %>
                <li><%= game %></li
             <% end %>
           </ul>
         <% end %>
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  3. #3
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also try using the Enumerable#group_by method.

    Assuming your games are in the @games instance variable:
    Code Ruby:
    <% @games.group_by(&:system).each do |system, games| -%>
      <h3><%= system %></h3>
      <ul>
        <% games.each do |game| -%>
        <li><%= game.title %></li>
        <% end -%>
      </ul>
    <% end -%>

    Untested, but I think you get the idea.

  4. #4
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies! I'll give this a shot when I get home this evening.

  5. #5
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again for the help all! This worked a charm, creating a new systems table, and then creating the relationship, as well as the nested each ... do loops.

    Now get ready for something even simpler: How do I, in my controller, only select systems that have at least one game?

    Here is the code in the view:

    Code Ruby:
    <% @systems.each do |system| %>
      <h3><%= system.name %></h3>
        <ul>
          <% system.games.each do |game| %>
    	  <li><%= game.title %></li>
          <% end %>
        </ul>
    <% end %>

    I tried to do a simple if ... then test to see if the count of games was more than 1, but the results showed all systems regardless.
    Code Ruby:
    <% @systems.each do |system| %>
      <h3><%= system.name %></h3>
      <ul>
        <% system.games.each do |game| %>
            <% if system.games.count > 0 %>
              <li><%= game.title %></li>
            <% else %>
              <% link_to 'Add a new game for this system', :action => 'add' %>
            <% end %>
          <% end %>
        </ul>
      <% end %>

    Perhaps I am overcomplicating the matter. Any help?

    Thanks again!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •