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

#changed? on host model does not detect changes in embeds_many association

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 7.0.5
    • Component/s: Associations
    • Environment:
      Ruby 2.5.0, Rails 6.0.0

      Given the minimal example

      class ShoppingCart
        include Mongoid::Document
      
        embeds_many :items
      end
      
      class Item
        include Mongoid::Document
      
        embedded_in :shopping_cart
      end
      

      when I create a cart with an item and then remove the item, changed? will still be false:

      2.5.0 :001 > cart = ShoppingCart.new
       => #<ShoppingCart _id: 5da45df1a8f8434462c1ce27, > 
      2.5.0 :002 > item = cart.items.new
       => #<Item _id: 5da45dfaa8f8434462c1ce28, > 
      2.5.0 :003 > cart.save
       => true 
      2.5.0 :004 > cart.items = []
       => [] 
      2.5.0 :005 > cart.changed?
       => false 
      

      This is especially problematic when using Mongoid::Timestamps, because cart.items.last.delete will $pull the item, but *not* $set the updated_at timestamp

            Assignee:
            Unassigned Unassigned
            Reporter:
            larnold@givve.com Leo Arnold
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: