From 94a1309194fa5962e33d395ede14e94b237c54f5 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Tue, 8 Aug 2006 16:59:06 +0000 Subject: Cache nil results for has_one associations so multiple calls don't call the database. Closes #5757. [Michael A. Schoen] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4721 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 10 +++++----- .../lib/active_record/associations/association_proxy.rb | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 009153d1d2..c803560413 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -656,7 +656,7 @@ module ActiveRecord module_eval do before_save <<-EOF association = instance_variable_get("@#{reflection.name}") - if !association.nil? + if association && association.target if association.new_record? association.save(true) end @@ -841,14 +841,14 @@ module ActiveRecord if association.nil? || force_reload association = association_proxy_class.new(self, reflection) retval = association.reload - unless retval.nil? - instance_variable_set("@#{reflection.name}", association) - else + if retval.nil? and association_proxy_class == BelongsToAssociation instance_variable_set("@#{reflection.name}", nil) return nil end + instance_variable_set("@#{reflection.name}", association) end - association + + association.target.nil? ? nil : association end define_method("#{reflection.name}=") do |new_value| diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 46804a3b93..a94daccd34 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -126,13 +126,13 @@ module ActiveRecord def load_target if !@owner.new_record? || foreign_key_present begin - @target = find_target if !loaded? + @target = find_target unless loaded? rescue ActiveRecord::RecordNotFound reset end end - loaded if target + loaded target end -- cgit v1.2.3