From b81d37fc029fad8c9ac0015310ca01daf5dcc0e7 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Mon, 21 Jun 2010 17:03:32 +0200 Subject: [PATCH] Fixing association bug to avoid searching when foreign key is nil --- lib/couchrest/model/associations.rb | 2 +- spec/couchrest/assocations_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/couchrest/model/associations.rb b/lib/couchrest/model/associations.rb index 6762250..6b97d6d 100644 --- a/lib/couchrest/model/associations.rb +++ b/lib/couchrest/model/associations.rb @@ -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 diff --git a/spec/couchrest/assocations_spec.rb b/spec/couchrest/assocations_spec.rb index e83e24a..61c6966 100644 --- a/spec/couchrest/assocations_spec.rb +++ b/spec/couchrest/assocations_spec.rb @@ -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