#1152 confirmed
Nick Plante

trying to trigger an additional save (without validations / callbacks) inside an after :save callback

Reported by Nick Plante | December 12th, 2009 @ 07:48 PM

This is admittedly a strange situation, but I was under the impression that this should work:


At the very least, the save! / update! operation shouldn't return true here.

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) February 5th, 2010 @ 02:50 AM

    • State changed from “unconfirmed” to “confirmed”

    This should work, but the bug is being caused by the new logic in 0.10.2 that allows the object graph to be saved.

    The new logic will traverse every dirty parent and child resource, and ask them to save. The problem with this though is that there can be circular relationships (eg. a company has n, employees and company belongs_to a primary contact employee). To combat circular saving problems there is a run_once method in Resource that only allows a specific method to be called one time in response to a save event. This bug is being caused because run_once has already recorded this resource as in the process of saving, so it won't allow the Resource#update! action to work.

    The run_once fix was originally a bit of a hack because the real solution is fairly difficult. We'd probably do something like they did with SQL Alchemy, with a UnitOfWork to handle saving dirty resources. The basic idea is that we'd open up a UnitOfWork session, traverse all the dirty parents and children and record them in the session. We'd then ask the session to save, and it would do a topological sort of the resources to ensure dependencies are resolved, and then save every object in the proper order.

    It will probably be some time before this is tackled, and may well be after DM 1.0 comes out. In the short term, you can save the objects using parts of the semipublic API:

    def update_status
      self.status = 'updated'
      ret = save_self(false)
      puts "set status of #{id} to '#{status}' in after save callback (update! returns #{ret})"
  • Dan Kubb (dkubb)

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Referenced by