Skip to content

Commit

Permalink
Raise DocumentNotFound instead of NoMethodErros:
Browse files Browse the repository at this point in the history
- When passing ids to nested attributes and no docs exist, grabbing the
  first class could throw a NoMethodError getting the object id state.
- Fixes #1608.
  • Loading branch information
durran committed Jan 19, 2012
1 parent c43187b commit c9ea7ae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -54,6 +54,11 @@ For instructions on upgrading to newer versions, visit

## 2.4.2 \[ In Development \] \[ Branch: 2.4.0-stable \]

### Resolved Issues

* \#1608 Guard against no method errors when passing ids in nested attributes
and the documents do not exist.

* \#1605 Remove deprecation warning on rescue responses, Rails 3.2

* \#1602 Preserve structure of $and and $or queries when typecasting.
Expand Down
4 changes: 3 additions & 1 deletion lib/mongoid/relations/builders/nested_attributes/many.rb
Expand Up @@ -92,7 +92,9 @@ def over_limit?(attributes)
def process(parent, attrs)
return if reject?(parent, attrs)
if id = attrs.extract_id
doc = existing.find(convert_id(existing.first.class, id))
first = existing.first
converted = first ? convert_id(first.class, id) : id
doc = existing.find(converted)
if destroyable?(attrs)
existing.delete(doc)
doc.destroy unless doc.embedded?
Expand Down
16 changes: 16 additions & 0 deletions spec/mongoid/nested_attributes_spec.rb
Expand Up @@ -2651,6 +2651,22 @@
person.posts(true).size.should == 2
end
end

context "when there are no documents" do

before do
person.posts.clear
end

it "raises a document not found error" do
expect {
person.posts_attributes =
{ "0" =>
{ "id" => BSON::ObjectId.new.to_s, "title" => "Rogue" }
}
}.to raise_error(Mongoid::Errors::DocumentNotFound)
end
end
end

context "when the ids do not match" do
Expand Down

0 comments on commit c9ea7ae

Please sign in to comment.