Rails belong_to has_many with a custom foreign key

I have solved the issue, it was a matter of adding each in the loop I implemented in the view:

<% @player.player_sessions.each do |player_session| %>
<ul>
    <li><h4>Highest Score:</h4> <%= player_session.total_score %> </li>
</ul>    
<% end %>

After playing around a bit, I realised i didn’t need to add the primary_key option in either of the views, and left the foreign key option only in the Player model.

class Player < ActiveRecord::Base
  set_primary_key "alias"
  attr_accessible :alias, :avatar
  has_many :player_sessions, :foreign_key => "player_alias", :class_name => "PlayerSession" 
end

class PlayerSession < ActiveRecord::Base
  attr_accessible :player_alias, :total_score
  belongs_to :player, :class_name => "Player" 
end

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)