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
This commit is contained in:
parent
2ce49e7dd5
commit
94a1309194
|
@ -1,5 +1,7 @@
|
||||||
*SVN*
|
*SVN*
|
||||||
|
|
||||||
|
* Cache nil results for has_one associations so multiple calls don't call the database. Closes #5757. [Michael A. Schoen]
|
||||||
|
|
||||||
* Add documentation for how to disable timestamps on a per model basis. Closes #5684. [matt@mattmargolis.net Marcel Molina Jr.]
|
* Add documentation for how to disable timestamps on a per model basis. Closes #5684. [matt@mattmargolis.net Marcel Molina Jr.]
|
||||||
|
|
||||||
* Don't save has_one associations unnecessarily. #5735 [Jonathan Viney]
|
* Don't save has_one associations unnecessarily. #5735 [Jonathan Viney]
|
||||||
|
|
|
@ -656,7 +656,7 @@ module ActiveRecord
|
||||||
module_eval do
|
module_eval do
|
||||||
before_save <<-EOF
|
before_save <<-EOF
|
||||||
association = instance_variable_get("@#{reflection.name}")
|
association = instance_variable_get("@#{reflection.name}")
|
||||||
if !association.nil?
|
if association && association.target
|
||||||
if association.new_record?
|
if association.new_record?
|
||||||
association.save(true)
|
association.save(true)
|
||||||
end
|
end
|
||||||
|
@ -841,14 +841,14 @@ module ActiveRecord
|
||||||
if association.nil? || force_reload
|
if association.nil? || force_reload
|
||||||
association = association_proxy_class.new(self, reflection)
|
association = association_proxy_class.new(self, reflection)
|
||||||
retval = association.reload
|
retval = association.reload
|
||||||
unless retval.nil?
|
if retval.nil? and association_proxy_class == BelongsToAssociation
|
||||||
instance_variable_set("@#{reflection.name}", association)
|
|
||||||
else
|
|
||||||
instance_variable_set("@#{reflection.name}", nil)
|
instance_variable_set("@#{reflection.name}", nil)
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
instance_variable_set("@#{reflection.name}", association)
|
||||||
end
|
end
|
||||||
association
|
|
||||||
|
association.target.nil? ? nil : association
|
||||||
end
|
end
|
||||||
|
|
||||||
define_method("#{reflection.name}=") do |new_value|
|
define_method("#{reflection.name}=") do |new_value|
|
||||||
|
|
|
@ -126,13 +126,13 @@ module ActiveRecord
|
||||||
def load_target
|
def load_target
|
||||||
if !@owner.new_record? || foreign_key_present
|
if !@owner.new_record? || foreign_key_present
|
||||||
begin
|
begin
|
||||||
@target = find_target if !loaded?
|
@target = find_target unless loaded?
|
||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
reset
|
reset
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
loaded if target
|
loaded
|
||||||
target
|
target
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -96,6 +96,11 @@ class HasOneAssociationsTest < Test::Unit::TestCase
|
||||||
assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit
|
assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_has_one_cache_nils
|
||||||
|
assert_nil companies(:another_firm).account
|
||||||
|
assert_queries(0) { companies(:another_firm).account }
|
||||||
|
end
|
||||||
|
|
||||||
def test_proxy_assignment
|
def test_proxy_assignment
|
||||||
company = companies(:first_firm)
|
company = companies(:first_firm)
|
||||||
assert_nothing_raised { company.account = company.account }
|
assert_nothing_raised { company.account = company.account }
|
||||||
|
|
Loading…
Reference in New Issue