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 657077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:47:02+00:00 2026-05-13T22:47:02+00:00

Is it possible to have multiple has_many :through relationships that pass through each other

  • 0

Is it possible to have multiple has_many :through relationships that pass through each other in Rails? I received the suggestion to do so as a solution for another question I posted, but have been unable to get it to work.

Friends are a cyclic association through a join table. The goal is to create a has_many :through for friends_comments, so I can take a User and do something like user.friends_comments to get all comments made by his friends in a single query.

class User
  has_many :friendships
  has_many :friends, 
           :through => :friendships,
           :conditions => "status = #{Friendship::FULL}"
  has_many :comments
  has_many :friends_comments, :through => :friends, :source => :comments
end

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"
end

This looks great, and makes sense, but isn’t working for me. This is the error I’m getting in relevant part when I try to access a user’s friends_comments:
ERROR: column users.user_id does not exist
: SELECT "comments".* FROM "comments" INNER JOIN "users" ON "comments".user_id = "users".id WHERE (("users".user_id = 1) AND ((status = 2)))

When I just enter user.friends, which works, this is the query it executes:
: SELECT "users".* FROM "users" INNER JOIN "friendships" ON "users".id = "friendships".friend_id WHERE (("friendships".user_id = 1) AND ((status = 2)))

So it seems like it’s entirely forgetting about the original has_many through friendship relationship, and then is inappropriately trying to use the User class as a join table.

Am I doing something wrong, or is this simply not possible?

  • 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-13T22:47:02+00:00Added an answer on May 13, 2026 at 10:47 pm

    Edit:

    Rails 3.1 supports nested associations. E.g:

    has_many :tasks
    has_many :assigments, :through => :tasks
    has_many :users, :through => :assignments
    

    There is no need for the solution given below. Refer to this screencast for more details.

    Original Answer

    You are passing a has_many :through association as a source for another has_many :through
    association. I don’t think it will work.

      has_many :friends, 
               :through => :friendships,
               :conditions => "status = #{Friendship::FULL}"
      has_many :friends_comments, :through => :friends, :source => :comments
    

    You have three approaches to solving this issue.

    1) Write an association extension

     has_many  :friends, 
               :through => :friendships,
               :conditions => "status = #{Friendship::FULL}" do
         def comments(reload=false)
           @comments = nil if reload 
           @comments ||=Comment.find_all_by_user_id(map(&:id))
         end
     end
    

    Now you can get the friends comments as follows:

    user.friends.comments
    

    2) Add a method to the User class.

      def friends_comments(reload=false)
        @friends_comments = nil if reload 
        @friends_comments ||=Comment.find_all_by_user_id(self.friend_ids)
      end
    

    Now you can get the friends comments as follows:

    user.friends_comments
    

    3) If you want this to be even more efficient then:

      def friends_comments(reload=false)
        @friends_comments = nil if reload 
        @friends_comments ||=Comment.all( 
                 :joins => "JOIN (SELECT friend_id AS user_id 
                                  FROM   friendships 
                                  WHERE  user_id = #{self.id}
                            ) AS friends ON comments.user_id = friends.user_id")
      end
    

    Now you can get the friends comments as follows:

    user.friends_comments
    

    All methods cache the results. If you want to reload the results do the following:

    user.friends_comments(true)
    user.friends.comments(true)
    

    OR better still:

    user.friends_comments(:reload)
    user.friends.comments(:reload)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have multiple view of the same display object? (e.g. same-computer
Is it possible to have multiple listeners to messages carried by MSMQ? WCF appears
Is it possible to have multiple folders where I can place applications to be
On IIS 6, is it possible to have multiple domain names pointing to the
A single Biztalk Server can have multiple Host processes. Is it possible to create
Is it possible to have a file belong to multiple subpackages? For example: /**
I have a model Foo that has_many 'Bar'. I have a factory_girl factory for
Is this possible? In an events system an event can have multiple times. (ie,
I have two models, associated with a HABTM (actually using has_many :through on both
So imagine you have multiple tables in your database each with it's own structure

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.