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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:23:45+00:00 2026-05-31T19:23:45+00:00

The rails docs are apparently in error – http://guides.rubyonrails.org/association_basics.html#selfjoins ) In designing a data

  • 0

The rails docs are apparently in error – http://guides.rubyonrails.org/association_basics.html#selfjoins)

In designing a data model, you will sometimes find a model that should
have a relation to itself. […]

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

With this setup, you can retrieve @employee.subordinates and
@employee.manager.


Actually, in at least the console, an error is generated in the above if foreign_key is not “employee_id”.

Here’s my specific code:

#Table name: plates
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  datetime   :datetime
#  parent_id  :integer
#  precision  :integer
#  tags       :string(255)
#  created_at :datetime        not null
#  updated_at :datetime        not null
#

class Plate < ActiveRecord::Base

  has_many :templates

  has_many :children, :class_name => "Plate"
  belongs_to :parent, :class_name => "Plate",
    :foreign_key => "parent_id"
  [...] 

…and the query I run:

irb(main):002:0> Plate.find_by_name("blog090822").children.first

If I run that it generates SQL looking for plate_id and then returns an error for non-existant column. If I change the column name to plate_id through a migration, reseed the DB and rerun the query it works.

If this is a rails documentation error, how commonplace is that.

  • 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-31T19:23:46+00:00Added an answer on May 31, 2026 at 7:23 pm

    In the API documents, there are some explanations to foreign_key. For belongs_to:

    By default this is guessed to be the name of the association with an “_id” suffix.
    

    For has_many:

    By default this is guessed to be the name of this class in lower-case and “_id” suffixed.
    

    So in your case, the foreign_key for :children should be plate_id, and the foreign_key for :parent should be parent_id.
    To let your code work while keeping the data schema, only the foreign_key for :children is necessary, which override the default foreign_key with parent_id.

    The code in the Rails guide could be wrong.

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

Sidebar

Related Questions

Rails has beautiful documentation on their guides and their website: http://guides.rubyonrails.org/association_basics.html I am taking
I looked in the Rails docs under inflector and found this message... Module deprecated
I have read through the Rails docs for Routing , Restful Resources , and
Rails uses the concept of migrations to deal with model changes using the ActiveRecord
I am going through the Rails API docs for collection_select and they are god-awful.
In the Rails docs for the asset pipeline, it states: The default behavior in
I have red docs on friendly_id for rails. It's easy: you set a certain
A javascript problem on a rails app. Twitter docs keep things very straightforward, however
I noticed in the Rails docs here that the update_attributes method is deprecated: This
Looking at the example of form_for from the rails docs , I see this

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.