title: Rails belong_to has_many with a custom foreign key
body: <p>I have a relationship between 2 models in my rails application. I have veered away from the standard of how to implement the relationship as I used another field as a primary key and the naming convention is different. Doing so resulted in the relationship seemingly not being established. I want to understand as to why.</p>
  
  <p>This is a trimmed down version of my models:</p>
  
  <pre><code>class Player &lt; ActiveRecord::Base
    set_primary_key "alias"
    attr_accessible :alias, :avatar
    has_many :player_sessions, :foreign_key =&gt; "player_alias", :class_name =&gt; "PlayerSession" 
  end
  
  class PlayerSession &lt; ActiveRecord::Base
    attr_accessible :player_alias, :total_score
    belongs_to :player, :foreign_key  =&gt; "player_alias", :class_name =&gt; "Player" 
  end
  </code></pre>
  
  <p>The <code>Player</code> model has the field <code>alias</code> which is the username in my application. I wanted the username to act as the primary key since it is unique and it would be easier to migrate the data and maintain relationships.</p>
  
  <p>Originally I only had PlayerSession model with data already filled in, but as my application grew I added the Player model and simply inserted a row with the same <code>alias</code>.</p>
  
  <p>In the <code>Player</code>'s <code>show</code> view I have the following code:</p>
  
  <pre><code>Player Sessions:
  &lt;% @player.player_sessions do |player_session| %&gt;
  &lt;ul&gt;
      &lt;li&gt;&lt;h4&gt;Highest Score:&lt;/h4&gt; &lt;%= player_session.total_score %&gt; &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>When I try to access the page it simple doesn't show the information.</p>
  
  <p>Other information I can add is that I haven't added any relationships in the database itself. </p>
  
  <p>I am still new to rails and still playing around with it. Any opinions that concern coding standards(outside from answering the question) are welcome.</p>
  
  <hr>
  
  <p><strong>Update</strong> I have implemented Babur Usenakunov's suggestion by adding the <code>primary_key</code> option in the models:</p>
  
  <pre><code>class Player &lt; ActiveRecord::Base
    set_primary_key "alias"
    attr_accessible :alias, :avatar
    has_many :player_sessions, :primary_key =&gt; "alias", :foreign_key =&gt; "player_alias", :class_name =&gt; "PlayerSession" 
  end
  
  class PlayerSession &lt; ActiveRecord::Base
    attr_accessible :player_alias, :total_score
    belongs_to :player, :primary_key =&gt; "alias", :foreign_key  =&gt; "player_alias", :class_name =&gt; "Player" 
  end
  </code></pre>
  
  <p>Also to test out the the data is valid I acquired the PlayerSession list manually:</p>
  
  <p>Code implemented in controller:</p>
  
  <pre><code>@player_sessions =  PlayerSession.where("player_alias = ?", params[:id])
  </code></pre>
  
  <p>Code implemented in view(which outputs the data):</p>
  
  <pre><code>&lt;% @player_sessions.each do |player_session| %&gt;
  &lt;ul&gt;
      &lt;li&gt;&lt;h4&gt;Highest Score:&lt;/h4&gt; &lt;%= player_session.total_score %&gt; &lt;/li&gt;
  &lt;/ul&gt;
  &lt;% end %&gt;
  </code></pre>