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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:47:28+00:00 2026-06-17T07:47:28+00:00

I have a situation where I would like to update a dependency of a

  • 0

I have a situation where I would like to update a dependency of a parent object after another object has been destroyed. Here is an example of the class hierarchy:

class Parent < ActiveRecord::Base
  has_one  :info, :dependent => :destroy
  has_many :conditions, :dependent => :destroy
  ....
end

class Info < ActiveRecord::Base
  belongs_to :parent

  def recalculate
    # Do stuff
  end
  ....
end

class Condition < ActiveRecord::Base
  belongs_to :parent
  has_one :condition_detail

  after_destroy :update_info

  def update_info
    parent.info.recalculate
    parent.info.save(:validate => false)
  end
  ....
end

The problem is that when the parent is destroyed, it destroys the condition, which then sets off the after_destroy callback and saves the info object after it was already destroyed. So after the parent is destroyed info still exists. If I don’t bypass validations, the save will silently fail, which I don’t want. And using save! raises an exception.

The callback on Condition has to be an after_destroy because otherwise the recalculate method on Info won’t have the correct representation of the state of the relationships to compute what it needs to.

I feel like I need a way to bypass callbacks when the parent is destroyed, but I don’t think that’s possible. I can’t use dependent => delete_all because that will not destroy the children of Condition. I tried seeing if there was a way I could tell if the parent had destroy called on it and use that info to bypass the save in the after_destroy, but that didn’t seem to work either.

Any help would be greatly appreciated, thanks!

  • 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-17T07:47:29+00:00Added an answer on June 17, 2026 at 7:47 am

    I see there being 2 options:

    1. Don’t use a after_destroy callback on Condition, but rather expect the info to be recalculated by whoever is destroying Condition. This is the cleanest, because you’re decoupling two separate intents: object destruction and calculation. You can see where this would be more helpful if, say, someday you want to destroy 2 conditions at once and only recalculate after both are destroyed. You cannot do this with a callback. It also aligns more closely with the Law of Demeter – the caller of Condition.destroy calling info.recalculate is better than Condition calling parent.info.recalculate.

      If you really want to package this behavior in Condition, create a #destroy_and_recalculate function that is called instead of just #destroy with a sort-of-hidden callback. It’s more obvious to callers that you are going to kick off a recalculation.

    2. Remove :dependent=>destroy on the parent’s :condition association, and replace it with your own before_destroy callback on Parent that will cause condition to be destroyed without callbacks.

      In Condition, I would create this method, say, #destroy_without_callbacks, and in it destroy Condition’s children, then cause condition to delete itself.

    The functionality of :dependent=>destroy is great, but with cycles like this, I think the clearest method by far is to make it very explicit what you’re doing by taking away some of the magic and managing the object and process lifecycle more explicitly.

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

Sidebar

Related Questions

How would I setup my controls for the following situation?: I have a parent-container
Here is the situation, I have UIViewController class with a UITableView outlet. I would
I would like to have a stored procedure that will update values in a
In a situation where you have a parent class which has one child class,
I have a table in MS SQL 2005. And would like to do: update
I have had several situations when i would like to do that. This could
I have a situation in which the ideal relationship, I believe, would involve Value
I am curious how others would handle this situation. I have a domain layer
i have situation like this: class IData { virtual void get() = 0; virtual
I have a situation where I need to do an update on a very

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.