Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6160299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:16:36+00:00 2026-05-23T21:16:36+00:00

In my Rails 3 project, I have a user model with a self referential

  • 0

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.

Here is the relevant schema for the relevant models:

  create_table "users", :force => true do |t|
    t.string   "email",                                 :default => "",    :null => false
    t.string   "first_name"
    t.string   "last_name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "follows", :force => 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 => true do |t|
    t.integer  "user_id"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Here’s the associations in the models

class User < ActiveRecord::Base
    has_many :follows
    has_many :followed_users, :through => :follows
  has_many :followed_activities, :through => :follows
    has_many :activities
end
class Follow < ActiveRecord::Base
    belongs_to :user
    belongs_to :followed_user, :class_name => "User"
    has_many :followed_activities, :primary_key => :followed_user, :foreign_key => :user_id, :class_name => "Activity"
end

The following work just fine:

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

However, the following just returns an empty array:

u.followed_activities

Here is the sql that is generated from the last statement:

  Activity Load (0.2ms)  SELECT `activities`.* FROM `activities` INNER JOIN `follows` ON `activities`.user_id = `follows`.id WHERE ((`follows`.user_id = 1))

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.

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.

Rails Version: 3.0.7

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:16:36+00:00Added an answer on May 23, 2026 at 9:16 pm

    Justin, you have 2 associations called “followed_activities”. sure, they have different context (different models), but I’d like to ask you to try method inside the association block like this:

    has_many :followed_users, :through => :follows do
      def activities
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a User model in rails with the following method: def current_project if(self.active_project
I have the following line of Rails 3 In the project.rb model this works
Starting a new rails project and we have a well-thought-out color palette, and want
This is not a ruby/rails project deploy. I have the following situation and I
I'm sure this is basic as I'm a new Rails user and want to
I have a rails 3.1 project using devise for authentication. I have several user
Rails newbie here and I'm stumped. In Rails I have a Project Model which
This is my first rails project and first rails post, so I might have
In my Rails project I have the following code: auto_link( h( wrap_long_string(post.text,50) )).gsub(/\n/,<br />)
I have a Rails project which I neglected to build tests for (for shame!)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.