Note that there are some explanatory texts on larger screens.

plurals
  1. POhas_many :through with :primary_key on join table not working
    primarykey
    data
    text
    <p>In my Rails 3 project, I have a user model with a self referential join, through the follow model. I want to use this join table to find activity related to the followed user. I have almost everything set up correctly, except that the query generated by the join is totally ignoring the :primary_key option on the join model.</p> <p>Here is the relevant schema for the relevant models:</p> <pre><code> create_table "users", :force =&gt; true do |t| t.string "email", :default =&gt; "", :null =&gt; false t.string "first_name" t.string "last_name" t.datetime "created_at" t.datetime "updated_at" end create_table "follows", :force =&gt; true do |t| t.integer "user_id" t.integer "followed_user_id" t.datetime "created_at" t.datetime "updated_at" end create_table "activities", :force =&gt; true do |t| t.integer "user_id" t.text "body" t.datetime "created_at" t.datetime "updated_at" end </code></pre> <p>Here's the associations in the models</p> <pre><code>class User &lt; ActiveRecord::Base has_many :follows has_many :followed_users, :through =&gt; :follows has_many :followed_activities, :through =&gt; :follows has_many :activities end class Follow &lt; ActiveRecord::Base belongs_to :user belongs_to :followed_user, :class_name =&gt; "User" has_many :followed_activities, :primary_key =&gt; :followed_user, :foreign_key =&gt; :user_id, :class_name =&gt; "Activity" end </code></pre> <p>The following work just fine:</p> <pre><code>u = User.first u.follows # returns corresponding records from the follows table u.followed_users # returns all users that u is following u.followed_users.first.activities # returns all activity records corresponding to the first person the user is following Follow.first.activities # same as the previous </code></pre> <p>However, the following just returns an empty array:</p> <pre><code>u.followed_activities </code></pre> <p>Here is the sql that is generated from the last statement:</p> <pre><code> Activity Load (0.2ms) SELECT `activities`.* FROM `activities` INNER JOIN `follows` ON `activities`.user_id = `follows`.id WHERE ((`follows`.user_id = 1)) </code></pre> <p>The reason it isn't working is because it is trying to join use 'follows'.id as the primary key rather than 'follows'.followed_user.</p> <p>Is this a bug, or do I have to repeat the :primary_key declaration somewhere on the user model? I can't find any mention anywhere in the Rails api, or anywhere else online.</p> <p>Rails Version: 3.0.7</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload