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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:24:21+00:00 2026-05-29T18:24:21+00:00

I’m having a hard time understanding how to implement a single model self-join in

  • 0

I’m having a hard time understanding how to implement a single model self-join in Rails.
The Guide to ActiveRecord Associations section 2.10 briefly explains Self-Joins but doesn’t offer enough information, and every example or post about this such references the Friendly Friend Railcast example that isn’t a single model self join, as described in the Rails Guide section 2.10.

The idea is a model that has_many and belongs_to itself, without needing a separate table for the relationship. The only reason I see for needing a separate table is if you want the relationship to contain more information than just the relationship. e.g. “best friends”, “barely know them”

I have a simple Post schema:

create_table "posts", :force => true do |t|
    t.datetime "posted"
    t.string   "nick"
    t.string   "title"
    t.text     "content"
    t.integer  "parent_post_id"
    t.datetime "created_at",     :null => false
    t.datetime "updated_at",     :null => false
end

The parent_post_id is a self-reference to other Post post_id’s. The posts.rb model has the relationship defined:

class Post < ActiveRecord::Base
  has_many :replies, :class_name => "Post"
  belongs_to :parent_post, :class_name => "Post",
    :foreign_key => "parent_post_id"
end

In the Controller or View I’m hoping to be able to do something like this:

@posts.each do |post|
  ...
  @replies = post.replies
  @replies.each do |reply|
    ...
  end
end

Or find a post’s parent:

@parent_post = post.parent_post

This may all be some syntax mis-understanding. So thanks in advance to anyone who can slap some sense into me. I’ve looked through every SO and blog post out there and none try the single model self-referential self-join method described in the Guide.

Points for anyone offering an explanation that doesn’t point to the Friendly Friend example that uses a separate relationship table.

  • 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-29T18:24:23+00:00Added an answer on May 29, 2026 at 6:24 pm

    I was missing the has_many foreign key to “parent_post_id”. Once its set, the post.replies will reference other Post instances by parent_post_id.

    The posts.rb model has the relationship defined:

    class Post < ActiveRecord::Base
      has_many :replies, :class_name => "Post",
        :foreign_key => "parent_post_id"
      belongs_to :parent_post, :class_name => "Post",
        :foreign_key => "parent_post_id"
    end
    

    I can now create Posts, assign a parent_post_id to a different Post, then later get all Posts that are replies to any parent Post.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.