{"issue":{"pull_request":{"diff_url":null,"html_url":null,"patch_url":null},"state":"open","user":{"avatar_url":"https://secure.gravatar.com/avatar/2ea33a461b2c20894f62958bcd9a4fb2?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"2ea33a461b2c20894f62958bcd9a4fb2","url":"https://api.github.com/users/gurgeous","login":"gurgeous","id":526195},"milestone":null,"labels":[{"name":"bug","color":"e10c02","url":"https://api.github.com/repos/mongoid/mongoid/labels/bug"},{"name":"confirmed","color":"e10c02","url":"https://api.github.com/repos/mongoid/mongoid/labels/confirmed"}],"assignee":null,"updated_at":"2011-11-11T18:23:23Z","comments":2,"closed_at":null,"url":"https://api.github.com/repos/mongoid/mongoid/issues/1424","body":"I often modify relations by setting the underlying relation_id field. I have to do this all over the place to work with forms in Rails. We're usually talking about hidden form fields that contain the id, populated via autocomplete, drag and drop, etc. Unfortunately setting the relation_id field doesn't flush the old relation proxy.\r\n\r\nTest:\r\n\r\n```ruby\r\nrequire \"rubygems\"\r\nrequire \"mongoid\"\r\n\r\nMongoid::Config.from_hash({\"database\" => \"mtest\"})\r\n\r\nclass Library\r\n  include Mongoid::Document\r\n  field :name\r\nend\r\nclass Book\r\n  include Mongoid::Document  \r\n  belongs_to :library\r\nend\r\n\r\none, two = Library.create(:name => \"one\"), Library.create(:name => \"two\")\r\n\r\nbook = Book.new(:library => one)\r\np book.library.name\r\nbook.library_id = two.id\r\np book.library.name\r\n```\r\n\r\nExpected:\r\n  \"one\"\r\n  \"two\"\r\n\r\nActual:\r\n  \"one\"\r\n  \"one\"\r\n\r\nThe workaround for this problem involves strategically calling reload_relations, like so:\r\n\r\n```ruby\r\nbook.attributes = params[:book]\r\nbook.reload_relations\r\n```\r\n\r\nUnfortunately that will also flush out any embedded relations that might've gotten populated by attrbitutes=, so sometimes I have to do something more strenuous:\r\n\r\n```ruby\r\nbook.attributes = params[:book]\r\nbook.attributes.each_pair do |key, value|\r\n  if key =~ /^(.*)_id$/\r\n    relation = $1\r\n    if book.instance_variable_defined?(\"@#{relation}\")          \r\n      book.remove_instance_variable(\"@#{relation}\")\r\n    end\r\n  end\r\nend\r\n```\r\n\r\nThis is especially difficult to deal with inside callbacks. I basically have to defensively clear out relations all over the place.","html_url":"https://github.com/mongoid/mongoid/issues/1424","title":"object.relation_id= should clear old relation","number":1424,"id":2211644,"created_at":"2011-11-11T17:39:52Z"},"action":"created","comment":{"user":{"avatar_url":"https://secure.gravatar.com/avatar/2ea33a461b2c20894f62958bcd9a4fb2?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"2ea33a461b2c20894f62958bcd9a4fb2","url":"https://api.github.com/users/gurgeous","login":"gurgeous","id":526195},"updated_at":"2011-11-11T18:23:23Z","url":"https://api.github.com/repos/mongoid/mongoid/issues/comments/2711406","body":"Filing the bug inspired me to finally create a comprehensive workaround:\r\n\r\n```ruby\r\nmodule Mongoid\r\n  module Attributes\r\n    alias :write_attribute_old :write_attribute\r\n\r\n    def write_attribute(name, value)\r\n      write_attribute_old(name, value)\r\n      \r\n      # amd - clear relation if necessary\r\n      if name.to_s =~ /^([a-z_]+)_id$/\r\n        relation = $1\r\n        if instance_variable_defined?(\"@#{relation}\")          \r\n          remove_instance_variable(\"@#{relation}\")\r\n        end\r\n      end\r\n    end\r\n  end\r\nend\r\n```\r\n\r\nI like to post these just to disgust Durran. :)","id":2711406,"created_at":"2011-11-11T18:23:23Z"}}