Fixing association bug to avoid searching when foreign key is nil

This commit is contained in:
Sam Lown 2010-06-21 17:03:32 +02:00
parent 226fe3bf38
commit b81d37fc02
2 changed files with 7 additions and 1 deletions

View file

@ -109,7 +109,7 @@ module CouchRest
base = options[:proxy] || options[:class_name]
class_eval <<-EOS, __FILE__, __LINE__ + 1
def #{attrib}
@#{attrib} ||= #{base}.get(self.#{options[:foreign_key]})
@#{attrib} ||= #{options[:foreign_key]}.nil? ? nil : #{base}.get(self.#{options[:foreign_key]})
end
EOS
end

View file

@ -62,6 +62,12 @@ describe "Assocations" do
@invoice.client_id.should be_nil
end
it "should not try to search for association if foreign_key is nil" do
@invoice.client_id = nil
Client.should_not_receive(:get)
@invoice.client
end
it "should raise error if class name does not exist" do
lambda {
class TestBadAssoc < CouchRest::Model::Base