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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:13:20+00:00 2026-05-27T07:13:20+00:00

Two models with the first being self-referential: def Page < ActiveRecord::Base has_many :source_page_relations, :class_name

  • 0

Two models with the first being self-referential:

def Page < ActiveRecord::Base
  has_many :source_page_relations,
           :class_name => 'PageRelation',
           :foreign_key => :child_id,
           :dependent => :destroy
  has_many :child_page_relations,
           :class_name => 'PageRelation',
           :foreign_key => :parent_id,
           :dependent => :destroy
  has_many :children, :through => :child_page_relations
  has_many :parents, :through => :source_page_relations
end

def PageRelation < ActiveRecord::Base
  belongs_to :parent, :class_name => 'Page', :foreign_key => :parent_id
  belongs_to :child,  :class_name => 'Page', :foreign_key => :child_id
end

Which means I can easily find both parents and children through @page.parents and @page.children.
Now, here’s the question: How do I find the “orphans” (or trunks, if you want to go tree-style, i.e. without parents) and the dead-ends (or leafs, i.e. without children) on a global basis?
I’m not that firm in SQL, so maybe someone has a fast idea how to accomplish that instead of a brute-force approach which iterates over all the pages?

  • 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-27T07:13:20+00:00Added an answer on May 27, 2026 at 7:13 am

    edit

    wait a minute ; there is, actually, a solution : use a left outer join.

    Page
      .joins("LEFT OUTER JOIN page_relations ON pages.id = page_relations.child_id")
      .where("page_relations.child_id IS NULL")
    

    this, for instance, will find all trunk pages (just joins all pages with their “parent” associations, and select pages with no association).

    i don’t know what impact it will have on performance, though ; i think it should be reasonably fast not too slow, but not to use as a common task. Maybe it would be simpler using an Arel Table (but i’m not familiar enough with these, sadly).


    i don’t know if there is any other way than brute-force iteration here (and if you find one, i’d like to know it).

    My advice would be to capture these properties as boolean attributes of the pages and use callbacks to maintain these in a coherent state.

    The idea is to add before_save, after_save, before_destroy… callbacks on the Page Model, so that every time we manipulate a page, we check if it is a “trunk” or a “leaf” (by checking if it has parents or children via parents.exist? and children.exists?) ; then we modify boolean trunk and leaf attributes on this page ( or on the associated page in case of a destroy ).

    This will somehow slow down performance on insert / update / delete, but allow to fetch trunks and leaves really fast with a simple where( trunk: true ) statement. You will probably have to use default_scope includes( :parents, :children ) on the Page model (or at least use includes a lot) to prevent the number of DB hits to explode.

    Maybe it’s possible to use the same strategy, but to place the callbacks on the PageRelation model ; it may even be possible to use an Observer. It all depends on your specific needs and coding style, and would be far to long to develop here.

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

Sidebar

Related Questions

I have two models, Article and Post that both inherit from a base model
I understand that fields such as Html.TextBox() accept two values, the first one being
I'm trying to export and import two related models using FasterCSV. The first model
I've got two models: Message and Attachment. Each attachment is attached to a specific
Suppose you have two models, User and City, joined by a third model CityPermission:
I've got two models, joined by a Has and Belongs To Many join table.
I have two models. We'll call them object A and object B. Their design
Say that I have two models- Users and Accounts. Each account can have at
I have two models indexed for searching (User and Item). I'm trying to do
I have two models, Room and Image . Image is a generic model that

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.