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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:18:02+00:00 2026-06-01T05:18:02+00:00

Wow I’ve been struggling with this for whole day, following the official ruby on

  • 0

Wow I’ve been struggling with this for whole day, following the “official” ruby on rails guides document, and just discovered that I might have been misguided by the document all along. I just want to confirm if this is true.

If you go to http://guides.rubyonrails.org/association_basics.html and under 2.10. self joins section it says:

class Employee < ActiveRecord::Base
  has_many :subordinates, :class_name => "Employee"
  belongs_to :manager, :class_name => "Employee",
    :foreign_key => "manager_id"
end

Now, I’m a newbie and just believed in this code (What else can I do?) and wrote some code that’s a variation of this self join case. However the more I looked at it the more it didn’t feel right. isn’t :subordinates supposed to have the :foreign_key field instead of :manager? Anyway I’ve just changed it so that the code is something like:

class Employee < ActiveRecord::Base
  has_many :subordinates, :class_name => "Employee", :foreign_key => "manager_id"
  belongs_to :manager, :class_name => "Employee"
end

and now it’s working. Am I missing something? Or is the official document wrong? It’s hard to believe that the official document would present incorrect information but maybe that’s the way it is.

  • 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-01T05:18:04+00:00Added an answer on June 1, 2026 at 5:18 am

    That’s right, the guide document is incorrect at the time of this writing.

    The belongs_to doesn’t need the :foreign_key option because AR will infer manager_id from the name of the association (“manager”). As documented, AR would raise an error when, given an Employee @dwight one attempts to @dwight.subordinates, because AR would use employee_id in the WHERE condition of the SELECT statement.

    According to the AR documentation passing the :foreign_key option to has_many results in declaring the FK that will be used when generating the query for @dwight.subordinates.

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

Sidebar

Related Questions

// Edit: oh wow. It's odd that I've been working on this for a
Wow, this should be so simple, but it' just not working. I need to
Wow i was just playing around with sp_addlinkedserver and i accidentally ran this command:
Wow I've been stuck on this one for days. I'm having trouble connecting to
Wow, my first Stackoverflow post, I've been watching this site for ages .. anyway,
Wow, what a mess. This is the scenario. Backbone driven JS app. RequireJS for
Oh wow is this one annoying me. Using c#, winforms, visual studio 2010 ulti.
It's really simple, I just want one screen. Wow, that [shiny thing] must have
Wow, I've never really used symlinks that much before, but this is really boggling:
Wow. I have been asked to add a 3rd level to a tree menu

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.