From b015a0f688ba99f290e78fcf04eaa4c2c43e6eed Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Tue, 9 Mar 2010 15:43:12 -0500 Subject: [PATCH] Allow setting of parent in belongs to related to nil --- lib/mongoid/associations/belongs_to_related.rb | 15 ++++++--------- .../associations/belongs_to_related_spec.rb | 4 ++++ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/mongoid/associations/belongs_to_related.rb b/lib/mongoid/associations/belongs_to_related.rb index 4aae9441eb..0944227042 100644 --- a/lib/mongoid/associations/belongs_to_related.rb +++ b/lib/mongoid/associations/belongs_to_related.rb @@ -41,19 +41,16 @@ def macro # # Options: # - # related: The related object - # parent: The parent +Document+ to update. + # target: The target(parent) object + # document: The +Document+ to update. # options: The association +Options+ # # Example: # - # BelongsToRelated.update(game, person, options) - def update(target, parent, options) - if target - parent.send("#{options.foreign_key}=", target.id) - return instantiate(parent, options, target) - end - target + # BelongsToRelated.update(person, game, options) + def update(target, document, options) + document.send("#{options.foreign_key}=", target ? target.id : nil) + instantiate(document, options, target) end end diff --git a/spec/unit/mongoid/associations/belongs_to_related_spec.rb b/spec/unit/mongoid/associations/belongs_to_related_spec.rb index 1669fc6b8c..f33382326f 100644 --- a/spec/unit/mongoid/associations/belongs_to_related_spec.rb +++ b/spec/unit/mongoid/associations/belongs_to_related_spec.rb @@ -134,6 +134,10 @@ def extension Mongoid::Associations::BelongsToRelated.update(nil, @child, @options).should be_nil end + it "removes the association" do + Mongoid::Associations::BelongsToRelated.update(nil, @child, @options) + @child.person.should be_nil + end end end