title: Ruby on Rails Nested Loop
body: <p>I'm fairly new to rails and I'm trying to figure out the best way to do this.</p>
  
  <p>I have a players table and a teams table. They both are HABTM with each other and use a join table.</p>
  
  <p>Models</p>
  
  <pre><code>class Player &lt; ActiveRecord::Base
    has_and_belongs_to_many :teams
  end
  
  class Team &lt; ActiveRecord::Base
    has_and_belongs_to_many :players
  end
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>def players
    @players = Player.all
  end
  </code></pre>
  
  <p>View</p>
  
  <pre><code>&lt;%@players.each do |player|%&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;%= link_to "Add", "steam://friends/add/#{player.steamid}"%&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= link_to player.name, player%&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%=player.email%&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%=player.teams.teamname%&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;%end%&gt;
  </code></pre>
  
  <p>First, I know teamname should be team_name.</p>
  
  <p>I've tried building a loop that loops through the teams but this page has over 1600 players and so it takes a few minutes to run it.</p>
  
  <p>Am I missing a better way to do this?  </p>