{"issue":{"title":"inconsistent behavior in update_attributes with multiple updates to a single reference field","user":{"login":"hankmoody","id":1187733,"url":"https://api.github.com/users/hankmoody","avatar_url":"https://secure.gravatar.com/avatar/85a59b9305445e7e9027ca15fac80155?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"85a59b9305445e7e9027ca15fac80155"},"created_at":"2011-12-29T02:15:05Z","milestone":null,"labels":[],"state":"open","updated_at":"2011-12-29T02:15:05Z","id":2676686,"closed_at":null,"html_url":"https://github.com/mongoid/mongoid/issues/1531","pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"body":"Either I have come across a bug or I'm not quite using update_attributes correctly. I have two models with many to many relatioship. User model has two virtual attributes (sports_tag_list and food_tag_list) both of which use 'tags'.\r\nI'm getting inconsistent behavior when I pass both virtual attributes at the same time to update_attributes. It doesn't seem to update the corresponding tag object in certain cases as shown in the test below...\r\n\r\n\r\n```ruby\r\nClass User\r\n\r\n    field :name\r\n    has_and_belongs_to_many :tags\r\n    attr_accessible :sports_tag_list, :food_tag_list, :name\r\n\r\n    def sports_tag_list=(tag_list)\r\n        # tag_list is a comma separated list.\r\n        # Parse list to individual tag strings\r\n        # to find_or_create tag objects from it.\r\n        # Collect all in tag_obj_array.\r\n        # So, for \"Golf, Tennis\", tag_obj_array\r\n        # will contain Tag objects for 'Golf' and 'Tennis'\r\n        # both with type 'sports'.\r\n        tag_obj_array.each do |tag|\r\n            self.tags.push(tag) unless self.tags.include?(tag)\r\n        end\r\n    end\r\n\r\n    def food_tag_list=(tag_list)\r\n        # Similar to the method above\r\n        # This will add tags with type \"food\"\r\n        tag_obj_array.each do |tag|\r\n            self.tags.push(tag) unless self.tags.include?(tag)\r\n        end       \r\n    end\r\n    \r\n    def sports_tag_list\r\n        # return comma delimited list of tags with type = 'sports'\r\n    end\r\n\r\n    def food_tag_list\r\n        # return comma delimited list of tags with type = 'food'\r\n    end\r\n\r\nend\r\n\r\nClass Tag\r\n    attr_accessible :name, :type\r\n    field :name\r\n    field :type\r\n    has_and_belongs_to_many :users\r\nend\r\n\r\n\r\n```\r\n\r\nTest Spec snippet:\r\n\r\n\r\n```ruby\r\nattrs = {:sports_tag_list => \"Golf\", :food_tag_list => \"Rice\"}\r\nu = User.create({:name => \"TestUser\"})\r\nu.update_attributes(attrs))\r\n\r\nTag.where(name: 'Golf').first.users.count.should == 1\r\nTag.where(name: 'Rice').first.users.count.should == 1\r\n\r\n# Remove 'Golf'\r\nu.update_attributes(attrs.merge(:sports_tag_list => \"\"))\r\n\r\nTag.where(name: 'Golf').first.users.count.should == 0\r\nTag.where(name: 'Rice').first.users.count.should == 1\r\n\r\n# Add 'Golf' back\r\nu.update_attributes(attrs)\r\n\r\nTag.where(name: 'Golf').first.users.count.should == 1   # THIS FAILS!! Actual value is 0 instead of 1.\r\nTag.where(name: 'Rice').first.users.count.should == 1\r\n\r\n```\r\n\r\nIs this a bug or update_attributes is limited to only updating a reference object only once in one call?\r\n","number":1531,"url":"https://api.github.com/repos/mongoid/mongoid/issues/1531","comments":0,"assignee":null},"action":"opened"}