aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-08-17 23:46:55 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-08-17 23:46:55 +0000
commit18057d2fb6017df988fadc6496f187c2e6208277 (patch)
treea98a31e6839a3383975aa0081e071a3e6343dd96 /activerecord
parent4584376a914f8a8cee059943d80ba73d2d4e2627 (diff)
downloadrails-18057d2fb6017df988fadc6496f187c2e6208277.tar.gz
rails-18057d2fb6017df988fadc6496f187c2e6208277.tar.bz2
rails-18057d2fb6017df988fadc6496f187c2e6208277.zip
Cache nil results for :included has_one associations also. Closes #5787.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4783 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG4
-rwxr-xr-xactiverecord/lib/active_record/associations.rb10
-rwxr-xr-xactiverecord/test/associations_test.rb3
3 files changed, 13 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index f5813a8d03..47e7531c68 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,6 +1,8 @@
*SVN*
-* Fixed a bug which would cause .save to fail after trying to access a empty has_one association on a unsaved record. [Tobias Luetke]
+* Cache nil results for :included has_one associations also. #5787 [Michael Schoen]
+
+* Fixed a bug which would cause .save to fail after trying to access a empty has_one association on a unsaved record. [Tobias Luetke]
* Nested classes are given table names prefixed by the singular form of the parent's table name. [Jeremy Kemper]
Example: Invoice::Lineitem is given table name invoice_lineitems
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 8f71734b3d..222bf6ca6a 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -870,7 +870,7 @@ module ActiveRecord
end
define_method("set_#{reflection.name}_target") do |target|
- return if target.nil?
+ return if target.nil? and association_proxy_class == BelongsToAssociation
association = association_proxy_class.new(self, reflection)
association.target = target
instance_variable_set("@#{reflection.name}", association)
@@ -1311,11 +1311,15 @@ module ActiveRecord
when :has_many, :has_and_belongs_to_many
collection = record.send(join.reflection.name)
collection.loaded
-
+
return nil if record.id.to_s != join.parent.record_id(row).to_s or row[join.aliased_primary_key].nil?
association = join.instantiate(row)
collection.target.push(association) unless collection.target.include?(association)
- when :has_one, :belongs_to
+ when :has_one
+ return if record.id.to_s != join.parent.record_id(row).to_s
+ association = join.instantiate(row) unless row[join.aliased_primary_key].nil?
+ record.send("set_#{join.reflection.name}_target", association)
+ when :belongs_to
return if record.id.to_s != join.parent.record_id(row).to_s or row[join.aliased_primary_key].nil?
association = join.instantiate(row)
record.send("set_#{join.reflection.name}_target", association)
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index cb704ffd2e..6e93b7430c 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -99,6 +99,9 @@ class HasOneAssociationsTest < Test::Unit::TestCase
def test_has_one_cache_nils
assert_nil companies(:another_firm).account
assert_queries(0) { companies(:another_firm).account }
+
+ firms = Firm.find(:all, :include => :account)
+ assert_queries(0) { firms.each(&:account) }
end
def test_proxy_assignment