Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3540

after_add callback triggered too early

      Hello, I have a cornercase problem which, if could be resolved in mongoid would make some people not waste about half a day as I did to track down the problem.

      So there is the structure

          class Transaction
            include Mongoid::Document
            has_many :events, :after_add => :some_method
      
            def some_method(event)
              if event.some_data
                #Do something
              end
            end
          end
      
          class Event
           include Mongoid::Document
           belongs_to :transaction
           attr_accessor :some_data 
          end
      

      Now, here is the trick, if you take an already persisted transaction

          transaction = Transaction.last
      

      and that you create an event children to the transaction,

          event = Event.new(:some_data => true, :transaction => transaction)
      

      will trigger some logic in the Transaction#some_method callback but

          event = Event.new(:transaction => transaction, :some_data => true)
      

      will trigger another one. The difference being only the order in which arguments are passed. Indeed, it seems that as soon as Event constructor hits the relation, the callback is triggered. That means that in the second case, :some_data is not yet set!

      Having consistent behavior between these two cases would be awesome.

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            muichkine Olivier Milla
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: