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

Adding or removing a document to embeds_one/embeds_many does not change updated_at on parent

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Associations

      Greetings,

      Adding a document to an embeds_many does not updated the parent's .updated_at. This example adds a child document, and saves the parent document. The updated_at field does not change. I am using "master"'s copy from today.

      This issue is similar to https://github.com/mongoid/mongoid/issues/1551

      require 'mongoid'
      
      Mongoid.load!('config/mongoid.yml', :test)
      
      # the big container object
      class Book
        include Mongoid::Document
        include Mongoid::Timestamps
        embeds_many :notes
      end
      
      # the template "note"
      class Note 
        include Mongoid::Document
        embedded_in :book
        field :message, :type => String
      end
      
      puts '--- A blank object'
      book = Book.new
      book.save
      puts "Changed? #{book.changed?}"
      puts "Updated at: #{book.updated_at}"
      puts "Document looks like: #{book.as_document}"
      
      sleep 3 # terrible
      puts "\n\n"
      
      puts '--- Adding a note'
      book.notes << Note.new({ :message => 'Hello World!'})
      book.save
      puts "Changed? #{book.changed?}"
      puts "Updated at: #{book.updated_at}"
      puts "Document looks like: #{book.as_document}"
      
      Unable to find source-code formatter for language: text. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      [deployer@local current]$ bundle exec ruby test.rb
      --- A blank object
      Changed? false
      Updated at: 2013-09-04 00:03:23 UTC
      Document looks like: {"_id"=>"522678cb9f4a1aeae7000001", "updated_at"=>2013-09-04 00:03:23 UTC, "created_at"=>2013-09-04 00:03:23 UTC}
      
      
      --- Adding a note
      Changed? false
      Updated at: 2013-09-04 00:03:23 UTC
      Document looks like: {"_id"=>"522678cb9f4a1aeae7000001", "updated_at"=>2013-09-04 00:03:23 UTC, "created_at"=>2013-09-04 00:03:23 UTC, "notes"=>[{"_id"=>"522678ce9f4a1aeae7000002", "message"=>"Hello World!"}]}
      

      Thank you!

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            danlo danlo
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: