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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:11:37+00:00 2026-05-25T01:11:37+00:00

I am new to Rails and finished Michael Hartl’s Ruby on Rails 3 Tutorial.

  • 0

I am new to Rails and finished Michael Hartl’s “Ruby on Rails 3 Tutorial”. Although the book teaches me a lot, I find this puzzle I don’t understand.

To preview the puzzle, that is, I don’t understand, inside User model,

has_many :following, :through=>:relationship, :source=>:followed

how this piece of code link “user.following” to an array of User instances.

And below is the whole puzzle.

First of all, I have the Relationship model, which records followed_id and follower_id infos. Inside Relationship model, the association is simple as

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id

  belongs_to :follower, :class_name => "User"
  belongs_to :followed, :class_name => "User"
end

Then, inside the User model, a user will assume the role of follower, and collect all its following rows in relationships table through relationships association.

class User < ActiveRecord::Base
  .
  .
  .
  has_many :relationships, :foreign_key => "follower_id", :dependent => :destroy
  .

Until now, I got it.

But confusion came at the next line, where through user.following it can assemble all that user’s following(User instances). Like so,

has_many :following, :through=>:relationships, :source=>:followed

I understand that :source=>:followed will overwrite the default, and let find all followed_ids associated with that user.

But, how can Rails recognize followed_id to link to User object? The label name doesn’t match users, nor is there :class_name specified. I just don’t get how Rails do this underlying work, or I missed out some hints.

Thank you! 🙂

  • 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-25T01:11:39+00:00Added an answer on May 25, 2026 at 1:11 am

    But, how can Rails recognize followed_id to link to User object? The
    label name doesn’t match users, nor is there :class_name specified. I
    just don’t get how Rails do this underlying work, or I missed out some
    hints.

    Rails recognize that is an user object because it is set in Relationship’s belongs_to. What Rails does here is to follow the relationship class through the foreign key “follower_id” and returning every User that has a relationship with the current user as followed. Of course Rails do that in a single SQL statement like this:

    SELECT `users`.* FROM `users` INNER JOIN `relationships` ON `relationships`.followed_id = `users`.id WHERE ((`relationships`.follower_id = <the current user id>  ))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Michael Hartl's Ruby on Rails Tutorial, ch 7.2.3 , rspec is returns the
I've set up a new Rails 2.3.2 app and added the Basecamp API ruby
I'm creating a new rails app for my business, and I just finished a
I am new to ruby. I've just installed Ruby and Rails on my fresh
I'm currently finishing up testing a new Ruby on Rails app. Just recently, some
I'm new to rails and I'm following the first tutorial on api.rubyonrails.org. However, I've
I run rails new search in my console, and I get all of this:
New Rails programmer here. There is probably a pretty simple solution to this, but
I'm following the Ruby on Rails 3 tutorial, chapter 6. Inside my directory spec/models/user_spec.rb
After starting a new Rails 3.0.5 project, I am surprised a little to find

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.