mongoid/mongoid — Issue #1745: update_attributes does a $pushAll on an embedded collection
Author: jazen
Created: 2012-02-22 11:33:38+00:00
State: open

This issue looks similar to previous ones, but i tried 2.4.3 and bleeding edge...

class Tag
include Mongoid::Document
field :name
embedded_in :taggable, polymorphic: true
end

class Category
include Mongoid::Document
embeds_many :tags, class_name: "Tag", as: :taggable
def tag_list
tags.map(&:name).join(",")
end
def tag_list=(s)
tags.clear
s.split(",").map(&:strip).each { |n| tags.new(name: n) }
end
end

Now trying:

c = Category.create
c.update_attributes :tag_list => "a"
c.reload
c.tag_list # => "a"

c.update_attributes :tag_list => "a,b"
c.reload
c.tag_list # => "a,a,b" :(

Looks like the embedded in collection isn't really cleared and Mongoid generates a $pushAll operation when updating?