Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3213814
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:59:16+00:00 2026-05-17T14:59:16+00:00

I am a Rails newbie (from PHP). So forgive this basic data structures question:

  • 0

I am a Rails newbie (from PHP). So forgive this basic data structures question:

In the controller:

@games = Game.all
@players = Player.all

In the view:

<% @games.each do |game| %>
  <%= game.player_id %>
<% end %>

When iterating over the @games, instead of displaying the player id with game.player_id, I’d like to display the player’s name which can be found in the Player object (:name).

How do I ‘find’ the correct player record by the id stored in game.player_id?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T14:59:17+00:00Added an answer on May 17, 2026 at 2:59 pm

    In the controller:

    @games = Game.all(:include => :player)
    

    In the view:

    <% @games.each do |game| %>
      <%= game.player.name %>    
    <% end %>
    

    Your data model looks odd to me. For the similar problem my data model might look like this:

    class Game < ActiveRecord::Base
      has_many :game_players
      has_many :players, :through => :game_players
    end
    
    class GamePlayer < ActiveRecord::Base
      belongs_to :game
      belongs_to :player
    end
    
    class Player < ActiveRecord::Base
      has_many :game_players
      has_many :games, :through => :game_players
    end
    

    Now in the controller I would query the games:

    @games = Game.all(:include => :players)
    

    In the view:

    <%@games.each do |game| %>
      <% games.players.each do |player| %>
        <%= player.name %>
      <%end%>
    <%end%>
    

    Edit 1

    If you have a concept of team then, I will introduce the team model:

    class Player < ActiveRecord::Base
      has_many :team_players
      has_many :teams, :through => :team_players
    end
    
    class TeamPlayer < ActiveRecord::Base
      belongs_to :player
      belongs_to :team
    end
    
    class Team < ActiveRecord::Base
      has_many :team_players
      has_many :players, :through => :team_players
      belongs_to :game
      # attributes name, score team size constraints etc.
    end
    
    class Game
      has_many :teams
      has_many :players, :through => :teams.
    end
    

    Adding a new game:

    @game = Game.new
    
    @team_a = @game.teams.build(:name => "Foo")
    @team_a.players << Player.find_all_by_name(["John", "Grace"])
    
    @team_b = @game.teams.build((:name => "Bar")
    @team_b.players << Player.find_all_by_name(["Kelly", "Yuvan"])
    
    @game.save
    

    While querying games in your controller:

    @games = Game.all(:include => [{:teams => :players}])
    

    In your view:

    <%@games.each do |game| %>
      <% games.teams.each do |team| %>
        <% team.players.each do |team| %>
          <%= player.name %>
        <%end%>
      <%end%>
    <%end%>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is one more newbie question: require 'tasks/rails' I saw this line in Rakefile
I'm a Git newbie. I recently moved a Rails project from Subversion to Git.
First of all, I'm a Rails newbie. I can hold my own in Ruby,
rails 3 newbie, with a general question about adding an additional route after scaffolding.
This is an honest question and I am not trolling. As a newbie to
Greetings all, newbie to Rails here. I'm currently having issues routing /profile to the
rails newbie here. I've started making a value based to-do list. So far I've
Complete rails newbie trying to get started. I have two classes, Ingredient, and Unit.
(Warning: Clueless Rails Newbie!) In my show.html.erb for my albums view, I call a
I'm Ruby/Rails newbie and trying to get the laika tool ( http://github.com/CCHIT/laika/tree/master ) running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.