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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:20:30+00:00 2026-06-17T00:20:30+00:00

I am trying to fetch self-referential data from Rails. I have a model called

  • 0

I am trying to fetch self-referential data from Rails. I have a model called Task with the following code:

class Task < ActiveRecord::Base
  has_many :tasks
  has_many :relationships, foreign_key: "follower_id", dependent: :destroy
  has_many :followed_tasks, through: :relationships, source: :followed
  scope :orphans, :conditions => "id NOT IN (SELECT DISTINCT followed_id FROM relationships)"

In my controller I would like to get all of the tasks with their children (subtasks) and grandchildren.
So far I have been doing this:

@tasks = Task.orphans.to_json(:include => { :followed_tasks => {
:include => :followed_tasks}})

That is probably not the most efficient way but it is one that I could come up with that works. Now if I want to have more levels of nesting this code quickly becomes hard to maintain. How could I improve my code to be able to specify a variable like maxLevelsNum = 4 and then have my query somehow fetch tasks with their subtasks four levels deep?

  • 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-17T00:20:31+00:00Added an answer on June 17, 2026 at 12:20 am

    It’s been a while since the question was asked, but, you can find below a possible answer (maxLevelsNum needs to be greater or equal to 1):

    hash_string = constructHash(maxLevelsNum)
    
    def constructHash(treeDepth)
      output = ":include => :followed_tasks"
    
      (2..treeDepth).each do 
        output = ":include => { :followed_tasks => {" + output + "}}"
      end
    
      return "{" + output + "}"
    end
    
    @tasks = Task.orphans.to_json(eval(hash_string))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to fetch wcf service from jquery. I have written below code
So I'm trying to fetch objects from core data. I have list of say
I have the following piece of code in which I am trying to fetch
I'm trying to fetch from Core Data records that have a startTime between two
I'm trying to fetch data from a page that resides on an intranet. The
I'm trying to fetch some JSON data from my server like so: var a
I'm trying to fetch data from a div (based on his id), using PHP's
I am trying to fetch an entity from the core data depending on the
I'm trying to fetch my data, but the results are empty! First, I have
When I'm trying to fetch data from the sqlite database table the NSArray has

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.