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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:03:17+00:00 2026-05-26T12:03:17+00:00

Requirements: I am building a task list application and wanted tasks to be able

  • 0

Requirements:

I am building a task list application and wanted tasks to be able to have sub-tasks.
I also wanted tasks to be able to exist in multiple places in the tree at once, for example, if I had the 2 tasks:

  1. Build dog kennel
  2. Put up new fence

If I planned on building the dog kennel out of the same material as the fence, both of these tasks would have a subtask of “Buy fence palings”.

My problematic implementation (feedback welcome):

I have 2 models:

  • Node (has_ancestry and belongs_to :task)
  • Task (has_many :nodes)

This means the tree (that allows me to have subtasks) does not store the task it self, just a reference to a task object.

Here is an example using the rails console:

t1 = Task.create :name => "Build dog kennel"
n1 = Node.create :task => t1

t2 = Task.create :name => "Put up new fence"
n2 = Node.create :task => t2

t3 = Task.create :name => "Buy fence palings"
n11 = Node.create :task => t3, :parent => n1
n21 = Node.create :task => t3, :parent => n2

t4 = Task.create :name => "Construct the fence"
n22 = Node.create :task => t4, :parent => n2

n2.children.each { |c| puts c.task.name }

This last line gives the following output, indicating a select n+1:

Node Load (0.2ms)  SELECT "nodes".* FROM "nodes" WHERE "nodes"."ancestry" = '12'
Task Load (0.2ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = 11 LIMIT 1
Buy fence palings
Task Load (0.2ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = 10 LIMIT 1
Put up new fence

Help?

I’m quite new to Ruby on Rails and ActiveRecord, but I would think all I need to do is join the nodes table with the tasks table based on the nodes.task_id foreign key, but I have looked through the Ancestry documentation and cant find anything useful.

In the future I plan on fetching more information from the task object via foreign keys too, such as author, related comments, etc. and with this implementation, one page load could trigger quite a lot of select queries 🙁

Can anyone offer me suggestions on how to accomplish this?
Is there a way to force eager loading? (Would that help?)
I’m open to feedback if you have a better idea how to accomplish this.

Thanks in advance!

  • 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-26T12:03:18+00:00Added an answer on May 26, 2026 at 12:03 pm

    So after playing around for a while, I finally found a way to do this.

    Instead of this line:

    n2.children.each { |c| puts c.task.name }
    

    which results in this:

    Node Load (0.2ms)  SELECT "nodes".* FROM "nodes" WHERE "nodes"."ancestry" = '27'
    Task Load (0.2ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = 23 LIMIT 1
    Buy fence palings
    Task Load (0.2ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = 24 LIMIT 1
    Construct the fence
    

    I used this line:

    n2.children.find(:all, :include => :task).each { |c| puts c.task.name }
    

    Which resulted in this:

    Node Load (0.2ms)  SELECT "nodes".* FROM "nodes" WHERE "nodes"."ancestry" = '27'
    Task Load (0.2ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" IN (23, 24)
    Buy fence palings
    Construct the fence
    

    This should only ever execute 2 queries, regardless of the size and the resulting set will include the tasks!
    I know this is probably basic stuff, but it may be a little confusing for new-comers like myself as the section of the rails guides that refers to eager loading only shows the class method includes()

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

Sidebar

Related Questions

I am building a web application that have several requirements for the client: 1.
I'm building a centralized desktop application using Python/wxPython. One of the requirements is User
Requirements Project should contain 1 main application and some secondary sub applications (which uses
I think about starting from scratch building a small application fullfilling two technical requirements:
I am building an application which has requirements stating that all browser features must
I'm in the requirements/design phase of building an application. We're using VS 2008 and
I'm building a new ASP.NET MVC application (in C#) and one of the requirements
I am in the process of building an application where the requirements are fuzzy.
I am building a DDD system and we have all the requirements on paper
I am building a cross platform product and one of the requirements is across

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.