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

Callbacks on embedded document prevent updating!

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 2.1.8
    • Component/s: None
    • Labels:
      None

      Description

      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')}

      ]}})

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved: