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

Callbacks on embedded document prevent updating!

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.1.8
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      The callback update somehow gets blended into the push operator. Example:

      class Person
        include Mongoid::Document
        has_and_belongs_to_many :awards, :inverse_of => nil
        embeds_many :somethings
      end
      
      class Award
        include Mongoid::Document
        has_many :persons, :inverse_of => :awards, :foreign_key => :award_ids
      end
      
      class Something
        include Mongoid::Document
        embedded_in :person
        
        field :name
        
        after_create :update_activity
        
        def update_activity
          person.awards.first.update_attributes(:updated_at => Time.now.utc)
        end
      end
      
          p = Person.create!
          a = Award.new
          p.awards << a
          
          s = Something.new :name => "first"
          p.somethings << s
          
          assert_equal 1, p.reload.somethings.count # this fails
      

      the mongo push command looks like this:
      MONGODB napa_test['people'].update(

      {"_id"=>BSON::ObjectId('4e3733f2803544b95c000002')}

      , {"$pushAll"=>{"updated_at"=>[

      {"name"=>"first", "_id"=>BSON::ObjectId('4e3733f2803544b95c000003')}

      ]}}) #notice the incorrect updated_at

      without the callback on Something, it looks like this, whish is obviously correct:
      MONGODB napa_test['people'].update(

      {"_id"=>BSON::ObjectId('4e3734ef803544b970000001')}

      , {"$pushAll"=>{"somethings"=>[

      {"name"=>"first", "_id"=>BSON::ObjectId('4e3734ef803544b970000003')}

      ]}})

            Assignee:
            Unassigned Unassigned
            Reporter:
            nader Nader Akhnoukh
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: