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?
In the controller:
In the view:
Your data model looks odd to me. For the similar problem my data model might look like this:
Now in the controller I would query the games:
In the view:
Edit 1
If you have a concept of team then, I will introduce the team model:
Adding a new game:
While querying games in your controller:
In your view: