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

  • Home
  • SEARCH
  • 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 7898647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:29:22+00:00 2026-06-03T08:29:22+00:00

I have two models: Users and Projects. The idea is that Users can follow

  • 0

I have two models: Users and Projects. The idea is that Users can follow both projects AND other users. Naturally, Users and Projects are part of a polymorphic “followable” type. Now, using the user model, I’d like to get three things:

user.followed_users
user.followed_projects
user.followers

The first two work fine; It’s the third that I’m having trouble with. This is sort of a reverse lookup where the foreign key becomes the “followable_id” column in the follows table, but no matter how I model it, I can’t get the query to run correctly.

User Model

has_many :follows, :dependent => :destroy
has_many :followed_projects, :through => :follows, :source => :followable, :source_type => "Project"
has_many :followed_users, :through => :follows, :source => :followable, :source_type => "User"
has_many :followers, :through => :follows, :as => :followable, :foreign_key => "followable", :source => :user, :class_name => "User"

Follow Model

class Follow < ActiveRecord::Base
  belongs_to :followable, :polymorphic => true
  belongs_to :user
end

My follows table has:
user_id
followable_id
followable_type

Whenever I run the query I get:

SELECT `users`.* FROM `users` INNER JOIN `follows` ON `users`.`id` = `follows`.`user_id` WHERE `follows`.`user_id` = 7

where it should be “followable_id = 7 AND followable_type = ‘User”, not “user_id = 7”

Any thoughts?

  • 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-06-03T08:29:24+00:00Added an answer on June 3, 2026 at 8:29 am

    Figured it out. Took a look at a sample project Michael Hartl made and noticed that the correct way to do this is to specify not only a relationship table (in this case follows) but also a reverse relationship table (which I called reverse follows).

        has_many    :follows,
                            :dependent => :destroy
    
        has_many    :followed_projects,
                            :through => :follows,
                            :source => :followable,
                            :source_type => "Project"
    
        has_many    :followed_users,
                            :through => :follows,
                            :source => :followable,
                            :source_type => "User"
    
        has_many    :reverse_follows,
                            :as => :followable,
                            :foreign_key => :followable_id,
                            :class_name => "Follow"
    
        has_many    :followers,
                            :through => :reverse_follows,
                            :source => :user
    

    Hope this helps some people out down the line!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Rails application, I have two models, Articles and Projects, which are both
I have two models - Tenant and User, each tenant will have_many users and
I have two models -- User and Entry -- that are related through a
I have two models User and Admin(with RailsAdmin) that use Devise. I sign in
I have two models: class Subscription belongs_to :subscriber, :class_name => User belongs_to :subscribable, :polymorphic
I have two entities, projects and users. These are modeled in Rails using Mongoid
I have for two similar models with one common Manager, that returns queryset for
Let's say I have two tables in a database: projects and users. I create
I have multiple projects that shares child apps with other projects . When working
I have two models, user and group. I also have a joining table groups_users.

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.