diff --git a/lib/couchrest/more/casted_model.rb b/lib/couchrest/more/casted_model.rb index 6740263..e2c5522 100644 --- a/lib/couchrest/more/casted_model.rb +++ b/lib/couchrest/more/casted_model.rb @@ -30,7 +30,7 @@ module CouchRest # Gets a reference to the top level extended # document that a model is saved inside of def base_doc - raise "Cannot call base_doc on a model that is not yet casted by a document" unless @casted_by + return nil unless @casted_by @casted_by.base_doc end diff --git a/lib/couchrest/support/rails.rb b/lib/couchrest/support/rails.rb index eaf1650..37b4c23 100644 --- a/lib/couchrest/support/rails.rb +++ b/lib/couchrest/support/rails.rb @@ -28,6 +28,7 @@ CouchRest::CastedModel.class_eval do # The to_param method is needed for rails to generate resourceful routes. # In your controller, remember that it's actually the id of the document. def id + return nil if base_doc.nil? base_doc.id end alias_method :to_param, :id diff --git a/spec/couchrest/more/casted_model_spec.rb b/spec/couchrest/more/casted_model_spec.rb index faf175d..9a22fee 100644 --- a/spec/couchrest/more/casted_model_spec.rb +++ b/spec/couchrest/more/casted_model_spec.rb @@ -326,6 +326,11 @@ describe CouchRest::CastedModel do @toy1.base_doc.title = 'Tom Foolery' @course.title.should == 'Tom Foolery' end + + it "should return nil if not yet casted" do + person = Person.new + person.base_doc.should == nil + end end describe "calling base_doc.save from a nested casted model" do