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

before_add and after_add callbacks get executed while they shouldn't

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.1.0
    • Affects Version/s: 4.0.0 final, 5.0.1, 5.0.0, 5.0.2
    • Component/s: None
    • None

      before_add and after_add callbacks get executed when a child calls the parent for the first time in a has_many relation.

      band.rb
      class Band
        include Mongoid::Document
      
        has_many :albums, before_add: :do_some_stuff, after_add: :notify_followers
      
        def do_some_stuff(album)
          puts "Doing some stuff"
        end
      
        def notify_followers(album)
          puts "Notifying followers"
        end
      end
      
      album.rb
      class Album
        include Mongoid::Document
      
        belongs_to :band
      end
      
      album = Album.first
      
      album.band
      Doing some stuff
      Notifying followers
      => #<Band _id: ... > 
      
      album.band
      => #<Band _id: ... > 
      

      I checked embedded_in and has_and_belongs_to_many relations with these callbacks, and they don't have this behaviour.

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            hmnhf Homayoon Hedayati Fard
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: