Skip to content

Commit

Permalink
Allow setting of parent in belongs to related to nil
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Mar 9, 2010
1 parent 6de7538 commit b015a0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
15 changes: 6 additions & 9 deletions lib/mongoid/associations/belongs_to_related.rb
Expand Up @@ -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:
#
# <tt>BelongsToRelated.update(game, person, options)</tt>
def update(target, parent, options)
if target
parent.send("#{options.foreign_key}=", target.id)
return instantiate(parent, options, target)
end
target
# <tt>BelongsToRelated.update(person, game, options)</tt>
def update(target, document, options)
document.send("#{options.foreign_key}=", target ? target.id : nil)
instantiate(document, options, target)
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/unit/mongoid/associations/belongs_to_related_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit b015a0f

Please sign in to comment.