diff --git a/lib/mongoid/associations/embeds_many.rb b/lib/mongoid/associations/embeds_many.rb index 832a2be3b8..fb86157eaf 100644 --- a/lib/mongoid/associations/embeds_many.rb +++ b/lib/mongoid/associations/embeds_many.rb @@ -82,8 +82,7 @@ def create(attrs = {}, type = nil) # The newly created Document. def create!(attrs = {}, type = nil) document = create(attrs, type) - errors = document.errors - raise Errors::Validations.new(errors) unless errors.empty? + raise Errors::Validations.new(document) unless document.errors.empty? document end diff --git a/lib/mongoid/errors.rb b/lib/mongoid/errors.rb index 367a3c2b30..9ef9c3cac2 100644 --- a/lib/mongoid/errors.rb +++ b/lib/mongoid/errors.rb @@ -61,10 +61,10 @@ def initialize(version) # # Validations.new(person.errors) class Validations < MongoidError - attr_reader :errors - def initialize(errors) - @errors = errors - super("Validation Failed: #{@errors.full_messages.join(", ")}") + attr_reader :document + def initialize(document) + @document = document + super("Validation Failed: #{@document.errors.full_messages.join(", ")}") end end diff --git a/lib/mongoid/persistence.rb b/lib/mongoid/persistence.rb index 50e7980724..d547c39b1a 100644 --- a/lib/mongoid/persistence.rb +++ b/lib/mongoid/persistence.rb @@ -209,7 +209,7 @@ def destroy_all(conditions = {}) # Raise an error if validation failed. def fail_validate!(document) - raise Errors::Validations.new(document.errors) + raise Errors::Validations.new(document) end end end diff --git a/spec/unit/mongoid/associations/embeds_many_spec.rb b/spec/unit/mongoid/associations/embeds_many_spec.rb index 324b2a30f8..4e01bb2461 100644 --- a/spec/unit/mongoid/associations/embeds_many_spec.rb +++ b/spec/unit/mongoid/associations/embeds_many_spec.rb @@ -215,7 +215,8 @@ Mongoid::Associations::Options.new(:name => :addresses) ) @errors = mock(:full_messages => ["test"], :empty? => false) - @address = mock(:parentize => true, :write_attributes => true, :errors => @errors, :_index= => true) + @address = mock(:parentize => true, :write_attributes => true, :_index= => true) + @address.expects(:errors).twice.returns(@errors) Address.expects(:instantiate).returns(@address) end @@ -492,13 +493,13 @@ def extension Mongoid::Associations::Options.new(:name => :addresses) ) end - + it "should update existing documents" do @association.nested_build({ "0" => { :street => "Yet Another" } }) @association.size.should == 2 @association[0].street.should == "Yet Another" end - + it "should create new documents" do @association.nested_build({ "2" => { :street => "Yet Another" } }) @association.size.should == 3 diff --git a/spec/unit/mongoid/errors_spec.rb b/spec/unit/mongoid/errors_spec.rb index cfdc28532b..46b3043921 100644 --- a/spec/unit/mongoid/errors_spec.rb +++ b/spec/unit/mongoid/errors_spec.rb @@ -73,13 +73,18 @@ context "default" do before do - @errors = stub(:full_messages => [ "Error 1", "Error 2" ]) - @error = Mongoid::Errors::Validations.new(@errors) + @errors = stub(:full_messages => [ "Error 1", "Error 2" ], :empty? => false) + @document = stub(:errors => @errors) + @error = Mongoid::Errors::Validations.new(@document) end it "contains the errors' full messages" do @error.message.should == "Validation Failed: Error 1, Error 2" end + + it "allows access to the invalid document" do + @error.document.should == @document + end end end end