Forum: Ruby on Rails Trying to show 3 tables in 1 view

Posted by Jax (Guest)
on 2013-02-15 09:59
(Received via mailing list)
Hi All,

I'm pretty new to ROR.

I got 3 tables:

Team, with columns name, world_ranking, star_player and img_url
Result with columns round, score and team_name
Player with columns first_name, last_name, position and player_number

my models relationships look like this

class Team < ActiveRecord::Base
  attr_accessible :image_url, :name, :star_player, :world_ranking
  has_many :players
  has_many :results

class Result < ActiveRecord::Base
  attr_accessible :round, :score, :team_name
  belongs_to :team
  has_many :players

class Player < ActiveRecord::Base
  attr_accessible :first_name, :last_name, :position, :player_number,
:team_name
  belongs_to :team
end

my default view is team#index and i have the option to show the team
what I want to achieve is to show the team details plus the team's 
players
and results for the tournament (pretty much the content of the other 2
tables) and am having an awful lot of trouble trying to render the info.
tried partials and a few methods but to no avail
Any ideas how can I go about this issue?

Thanking you in advance!

Jax
Posted by Lorenz Blackbird (ratman)
on 2013-02-15 10:52
In the index view of team write the code of index view of player and 
select only the team players by @team.player:

<table>
  <tr>
    <th>Name</th>
    <th>Position</th>
    <th>Number</th>
  </tr>
  <% @team.players.each do |team_player| %>
    <tr>
      <td><%= team_player.first_name %> <%= team_player.last_name 
%></td>
      <td><%= team_player.position %></td>
      <td><%= team_player.number %></td>
    </tr>
  <% end %>
</table>

For the results it's he same.
Posted by Jax (Guest)
on 2013-02-16 13:16
(Received via mailing list)
It worked a treat! thanks a million!

Jax
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.