Reputation: 3324
I have 3 models that are interacting:
Sessions
Session
has multiple SessionPlayer
sSessionPlayer
belongs to Player
On the associated Session index, I am trying to display the list of session players for a given session, and each session player's player name -- session_player.player.name
. Running the Rails page provides me with the proper data, but during testing I am having an issue.
In the index.html.erb page I am using the following loop:
<% @sessions.each do |session| %>
<% session.session_player.each do |player| %>
<div><%= player.player.name %></div>
<% end %>
<% end %>
When the code is executed during test, it is getting a null exception on player.player
, however I can debug it and it has a value set up properly for player.player_id
leading me to believe there is something wrong with my model code or some strange caching going on during the test.
Session
model
class Session < ActiveRecord::Base
has_many :session_player
has_many :player, through: :session_player
end
SessionPlayer
model
class SessionPlayer < ActiveRecord::Base
belongs_to :player
belongs_to :session
end
Player
model
class Player < ActiveRecord::Base
has_many :session_player
has_many :session, through: :session_player
end
The test and fixture data are both boilerplate created via RubyMine (as were the controllers and models), but I can post those if need be.
Upvotes: 0
Views: 247
Reputation: 17834
I think the problem is that you used session_player
, but it should be session_players
, use this line instead
<% session.session_players.each do |player| %>
And in your model, since you are using has_many
, you should use pluralized form of model name
has_many :session_players
Upvotes: 3