diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-04-14 07:49:13 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-04-14 07:49:13 +0000 |
commit | 75b8ac80d988c39e48966d6b60f0d1d46c0d75a6 (patch) | |
tree | a30b3f7ad12046afe56ca2c89c86a89eec497589 /activerecord | |
parent | cef09015c7264adf082bff01a220a0b66a83dd90 (diff) | |
download | rails-75b8ac80d988c39e48966d6b60f0d1d46c0d75a6.tar.gz rails-75b8ac80d988c39e48966d6b60f0d1d46c0d75a6.tar.bz2 rails-75b8ac80d988c39e48966d6b60f0d1d46c0d75a6.zip |
Dont initialize the association proxy for nil records
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1165 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 1 | ||||
-rw-r--r-- | activerecord/test/associations_go_eager_test.rb | 4 | ||||
-rw-r--r-- | activerecord/test/fixtures/posts.yml | 7 |
3 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 1b78b02e84..b1daf3df1e 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -557,6 +557,7 @@ module ActiveRecord end define_method("set_#{association_name}_target") do |target| + return if target.nil? association = association_proxy_class.new(self, association_name, association_class_name, association_class_primary_key_name, options) diff --git a/activerecord/test/associations_go_eager_test.rb b/activerecord/test/associations_go_eager_test.rb index 36f79265a5..498d557a02 100644 --- a/activerecord/test/associations_go_eager_test.rb +++ b/activerecord/test/associations_go_eager_test.rb @@ -29,6 +29,10 @@ class EagerAssociationTest < Test::Unit::TestCase assert_equal 2, posts.first.comments.size end + def test_loading_with_no_associations + assert_nil Post.find(@authorless.id, :include => :author).author + end + def test_eager_association_loading_with_belongs_to comments = Comment.find(:all, :include => :post) assert_equal @welcome.title, comments.first.post.title diff --git a/activerecord/test/fixtures/posts.yml b/activerecord/test/fixtures/posts.yml index f5effb20fc..9d22b2a603 100644 --- a/activerecord/test/fixtures/posts.yml +++ b/activerecord/test/fixtures/posts.yml @@ -11,3 +11,10 @@ thinking: title: So I was thinking body: Like I hopefully always am type: SpecialPost + +authorless: + id: 3 + author_id: "" + title: I don't have any comments + body: I just don't want to + type: Post |