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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:58:32+00:00 2026-06-08T00:58:32+00:00

I feel the following self join model example given on http://guides.rubyonrails.org/association_basics.html#self-joins is incorrect. class

  • 0

I feel the following self join model example given on http://guides.rubyonrails.org/association_basics.html#self-joins is incorrect.

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

I feel it should be as following. Can you please suggest which one is correct and why?

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

My rationale: The model bearing belongs_to relation carries the foreign_key for the model it references to.

  • 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-08T00:58:34+00:00Added an answer on June 8, 2026 at 12:58 am

    The example is correct.

    The “convention over configuration” mantra applies here, you only need to specify what the foreign key if it is not "#{name_of_association}_id"

    Therefore belongs_to :manager, :class_name => "Employee" implies the foreign key is manager_id

    However has_many :subordinates, :class_name => "Employee" assumes subordinates_id to be the childrens’ foreign key, which is why if must be defined. The definition of :foreign_key in a has_many will be for the child and will join against the current model’s id.

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

Sidebar

Related Questions

I am faced with the following issue and at this point I feel like
Im sorry, i feel kind of dumb, but im having trouble implementing the following
Right now, I have the following class methods: def check_capacity(self, at_index) def update_capacity(self, at_index)
I feel like the following code is suboptimal, and that it could be better.
I feel like there must be a much better way of expressing the following
I feel really stupid right now. The following code should output 'If you can
I have a class like the following: class A: def __init__(self, arg1, arg2, arg3):
Feel free to close and/or redirect if this has been asked, but here's my
Feel free to edit the title, engrish can sometimes confuse instead of help. I
I feel like I am not writing this correctly and this is my first

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.