aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-09-26 06:56:17 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-09-26 06:56:17 +0000
commit1caa76304b6eef3d637d861585bda72ed367567f (patch)
tree59e6c69bc870012a6104670dd92abc0d2145425b /activerecord
parente5a0a2732718bebf17e43bf817864561daeada3f (diff)
downloadrails-1caa76304b6eef3d637d861585bda72ed367567f.tar.gz
rails-1caa76304b6eef3d637d861585bda72ed367567f.tar.bz2
rails-1caa76304b6eef3d637d861585bda72ed367567f.zip
has_one associations with a nil target may be safely marshaled. Closes #6279.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5188 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/association_proxy.rb2
-rwxr-xr-xactiverecord/test/associations_test.rb17
3 files changed, 19 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 5f6c6e8931..da4e0a59d2 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* has_one associations with a nil target may be safely marshaled. #6279 [norbauer, Jeremy Kemper]
+
* Duplicate the hash provided to AR::Base#to_xml to prevent unexpected side effects [Koz]
* Add a :namespace option to AR::Base#to_xml [Koz]
diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb
index ac7188d712..4cdbc52ccf 100644
--- a/activerecord/lib/active_record/associations/association_proxy.rb
+++ b/activerecord/lib/active_record/associations/association_proxy.rb
@@ -125,6 +125,8 @@ module ActiveRecord
end
def load_target
+ return nil unless defined?(@loaded)
+
if !loaded? and (!@owner.new_record? || foreign_key_present)
@target = find_target
end
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index aaafb0e396..5e9a39873d 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -102,13 +102,26 @@ class HasOneAssociationsTest < Test::Unit::TestCase
end
def test_has_one_cache_nils
- assert_nil companies(:another_firm).account
- assert_queries(0) { companies(:another_firm).account }
+ firm = companies(:another_firm)
+ assert_queries(1) { assert_nil firm.account }
+ assert_queries(0) { assert_nil firm.account }
firms = Firm.find(:all, :include => :account)
assert_queries(0) { firms.each(&:account) }
end
+ def test_can_marshal_has_one_association_with_nil_target
+ firm = Firm.new
+ assert_nothing_raised do
+ assert_equal firm.attributes, Marshal.load(Marshal.dump(firm)).attributes
+ end
+
+ firm.account
+ assert_nothing_raised do
+ assert_equal firm.attributes, Marshal.load(Marshal.dump(firm)).attributes
+ end
+ end
+
def test_proxy_assignment
company = companies(:first_firm)
assert_nothing_raised { company.account = company.account }