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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:57:30+00:00 2026-05-11T17:57:30+00:00

I’m building a small twitter style microblogging service where users can follow other users

  • 0

I’m building a small twitter style microblogging service where users can follow other users and get a feed of their messages

I have the following models:

class Follow < ActiveRecord::Base
  belongs_to :follower, :class_name => "User"
  belongs_to :followee, :class_name => "User"
end

class User < ActiveRecord::Base
  has_many :follows,   :foreign_key => 'follower_id',
                       :class_name => 'Follow'
  has_many :followers, :through => :follows
  has_many :followed,  :foreign_key => 'followee_id',
                       :class_name => 'Follow'
  has_many :followees, :through => :followed
  has_many :messages
end

class Message < ActiveRecord::Base
  belongs_to :user
end

To get a feed for the current user, I want to perform the following SQL query:

SELECT * FROM follows JOIN users JOIN messages WHERE follows.follower_id = current_user.id AND follows.followee_id = users.id AND users.id = messages.user_id;

What is the correct ActiveRecord way of doing this?

  • 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-11T17:57:30+00:00Added an answer on May 11, 2026 at 5:57 pm

    Not sure what you’re looking for, but here is my suggestion:

    I assume that you have other purposes for that Follow class, otherwise I don’t see the purpose of it.

    The “correct way” (i.e. my completely subjective way) to do it would actually be something like this:

    class User < ActiveRecord::Base
      has_and_belongs_to_many :followers, :foreign_key => 'followed_id', 
          :class_name => 'User', :association_foreign_key => 'follower_id', 
          :include => [:messages]
      has_and_belongs_to_many :follows, :foreign_key => 'follower_id', 
          :class_name => 'User', :association_foreign_key => 'followed_id'
      has_many :messages
    end
    
    class Message < ActiveRecord::Base
      belongs_to :user
    end
    

    Then create the following table:

    create_table :users_users, :id => false do |t|
      t.integer :followed_id
      t.integer :follower_id
    end
    

    And you’re set:

    followed = User.find :first
    follower = User.find :last
    
    followed.followers << follower
    
    followed.followers.first.messages
    followed.followers.first.followers.first.messages # etc...
    

    But from what I make it, you want to show all the messages from all the followers at the same time.

    This should be possible to achieve by adding

     has_and_belongs_to_many :followed_messages, :foreign_key => 'follower_id', 
      :class_name => 'Message', :association_foreign_key => 'followed_id'
    

    to the User class, but I don’t know how correct that way would be. Or it might be possible to achieve with association extensions but there I can’t really give any examples.

    Update:
    By changing the :class_name, it will associate it with the Message.id, didn’t think about that so it will not be correct in this way.

    So the only “nice” option is to go through the User class like in the first example.
    The only other options I can see is either the association extensions (which I can’t give you an example for) or perhaps using a finder statement.

     has_many :followed_messages, :class_name => 'Message',
      :finder_sql => 'select * from messages where user_id in(select followed_id from users_users where follower_id = #{id})'
    

    You probably have to customize that sql statement to get everything to work, but at least you should get the picture 🙂

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.