title: Odd behavior in Mongoid: after_add callback fired when accessing referenced model
body: <p>The model's callback is being fired after accessing a referenced document. It is odd behavior and I am not sure why this happens.</p>
  
  <p>Here is a reproducible example:</p>
  
  <pre><code>class Group
      include Mongoid::Document
      include Mongoid::Timestamps
  
      belongs_to :domain_group, :class_name =&gt; 'Domain', :inverse_of =&gt; :child_groups
  
      field :name
  end
  
  class Domain &lt; Group
  
      has_many :child_groups, :class_name =&gt; 'Group', :inverse_of =&gt; :domain_group, after_add: :print_childgroup
  
      field :sid
  
      def print_childgroup(group)
          puts 'calling print_childgroup()'
      end
  end
  </code></pre>
  
  <p>Route handling:</p>
  
  <pre><code>get "/temp_init" do
      d = Domain.create(name: 'Domain', sid: '23Asdfai9')
      Group.create(name: 'group1', domain_group: d)
      Group.create(name: 'group2', domain_group: d)
  end
  
  get "/view" do
      g = Group.where(name: 'group1').first
      d = g.domain_group
  end
  </code></pre>
  
  <p>To see it in action:</p>
  
  <ul>
  <li><p>Visit <code>/temp_init</code> to create seed values and console shows:</p>
  
  <pre><code>calling print_childgroup()
  calling print_childgroup()
  </code></pre>
  
  <p>This is expected since we've just added two groups, so the <code>after_add</code> callback is fired twice.</p></li>
  <li><p>Visit <code>/view</code> and the console output will show:</p>
  
  <pre><code>calling print_childgroup()
  </code></pre>
  
  <p><strong>This is NOT expected</strong></p>
  
  <p>The offending line is <code>g.domain_group</code>, which triggers the <code>after_add</code> callback for some reason.</p></li>
  </ul>
  
  <p>My current workaround is this:</p>
  
  <pre><code># Grab the domain group id
  d_id = g.domain_group_id
  
  d = Domain.find(d_id)
  # Do stuff with d object
  </code></pre>
  
  <p>Which works, but it is ugly and seems like it is making an extra query. Why does <code>g.domain_group</code> fire the add callback and is there a proper workaround?</p>