mardi 18 novembre 2014

ActiveRecord has_many association null


Vote count:

0




I have 3 models that are interacting:



  • Sessions

  • Each Session has multiple SessionPlayers

  • Each SessionPlayer 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.



asked 1 min ago







ActiveRecord has_many association null

Aucun commentaire:

Enregistrer un commentaire