diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index 7b66e7c..2321894 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -92,6 +92,7 @@ module Cocoon def create_object(f, association) assoc = f.object.class.reflect_on_association(association) + raise "Association #{association} doesn't exist on #{f.object.class}" unless assoc if assoc.class.name == "Mongoid::Relations::Metadata" conditions = assoc.respond_to?(:conditions) ? assoc.conditions.flatten : [] assoc.klass.new(*conditions) diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb index 355d650..cc1a61a 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -191,6 +191,10 @@ describe Cocoon do result = @tester.create_object(stub(:object => Comment.new), :post) result.should be_a Post end + + it "should raise error if cannot reflect on association" do + expect { @tester.create_object(stub(:object => Comment.new), :not_existing) }.to raise_error /exist/ + end end context "get_partial_path" do