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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:40:07+00:00 2026-05-10T16:40:07+00:00

I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => ‘sort_order’ end

  • 0

I have this Task model:

class Task < ActiveRecord::Base   acts_as_tree :order => 'sort_order' end 

And I have this test

class TaskTest < Test::Unit::TestCase   def setup     @root = create_root   end    def test_destroying_a_task_should_destroy_all_of_its_descendants     d1 = create_task(:parent_id => @root.id, :sort_order => 2)     d2 = create_task(:parent_id => d1.id, :sort_order => 3)     d3 = create_task(:parent_id => d2.id, :sort_order => 4)     d4 = create_task(:parent_id => d1.id, :sort_order => 5)     assert_equal 5, Task.count      d1.destroy      assert_equal @root, Task.find(:first)     assert_equal 1, Task.count   end end 

The test is successful: when I destroy d1, it destroys all the descendants of d1. Thus, after the destroy only the root is left.

However, this test is now failing after I have added a before_save callback to the Task. This is the code I added to Task:

before_save :update_descendants_if_necessary  def update_descendants_if_necessary   handle_parent_id_change if self.parent_id_changed?   return true end  def handle_parent_id_change   self.children.each do |sub_task|     #the code within the loop is deliberately commented out   end end 

When I added this code, assert_equal 1, Task.count fails, with Task.count == 4. I think self.children under handled_parent_id_change is the culprit, because when I comment out the self.children.each do |sub_task| block, the test passes again.

Any ideas?

  • 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. 2026-05-10T16:40:08+00:00Added an answer on May 10, 2026 at 4:40 pm

    I found the bug. The line

    d1 = create_task(:parent_id => @root.id, :sort_order => 2) 

    creates d1. This calls the before_save callback, which in turn calls self.children. As Orion pointed out, this caches the children of d1.

    However, at this point, d1 doesn’t have any children yet. So d1’s cache of children is empty.

    Thus, when I try to destroy d1, the program tries to destroy d1’s children. It encounters the cache, finds that it is empty, and a result doesn’t destroy d2, d3, and d4.

    I solved this by changing the task creations like this:

    @root.children << (d1 = new_task(:sort_order => 2)) @root.save! 

    This worked so I’m ok with it 🙂 I think it is also possible to fix this by either reloading d1 (d1.reload) or self.children (self.children(true)) although I didn’t try any of these solutions.

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

Sidebar

Ask A Question

Stats

  • Questions 60k
  • Answers 60k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Are you closing the response stream and disposing of the… May 11, 2026 at 9:31 am
  • added an answer A few ways you could achieve this: 1. Do the… May 11, 2026 at 9:31 am
  • added an answer Mono 2.4 will ship with NUnit 2.4.8, and has been… May 11, 2026 at 9:31 am

Related Questions

I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
For customer service week this year, I have the privileged task of creating a
I have this code in jQuery, that I want to reimplement with the prototype
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this line in a javascript block in a page: res = foo('<%=
I have this setup where in my development copy I can commit changes on
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the
I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.