diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-02-27 22:41:35 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-02-27 22:41:35 +0000 |
commit | 558c5ff251fa790cd0d623468cc0e45d7efa45ed (patch) | |
tree | 2578527fb6f7e658d1b3960b07c73256b870f24b /activerecord | |
parent | 0d70f31057ff5cbdd7f62708a62ce846a1811f9c (diff) | |
download | rails-558c5ff251fa790cd0d623468cc0e45d7efa45ed.tar.gz rails-558c5ff251fa790cd0d623468cc0e45d7efa45ed.tar.bz2 rails-558c5ff251fa790cd0d623468cc0e45d7efa45ed.zip |
Fix preloading nil polymorphic belongs_to. Closes #11218 [matrix9180]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8933 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 11 | ||||
-rw-r--r-- | activerecord/test/cases/associations/join_model_test.rb | 8 | ||||
-rw-r--r-- | activerecord/test/fixtures/taggings.yml | 5 |
3 files changed, 17 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 313c51dae0..a89f5f5459 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -162,12 +162,13 @@ module ActiveRecord # Construct a mapping from klass to a list of ids to load and a mapping of those ids back to their parent_records records.each do |record| - klass = record.send(polymorph_type) - klass_id = record.send(primary_key_name) + if klass = record.send(polymorph_type) + klass_id = record.send(primary_key_name) - id_map = klasses_and_ids[klass] ||= {} - id_list_for_klass_id = (id_map[klass_id] ||= []) - id_list_for_klass_id << record + id_map = klasses_and_ids[klass] ||= {} + id_list_for_klass_id = (id_map[klass_id] ||= []) + id_list_for_klass_id << record + end end klasses_and_ids = klasses_and_ids.to_a else diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb index e72c6d0e07..e128d6dda3 100644 --- a/activerecord/test/cases/associations/join_model_test.rb +++ b/activerecord/test/cases/associations/join_model_test.rb @@ -331,7 +331,7 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase assert_no_queries do assert_equal desired, tag_with_include.tagged_posts end - assert_equal 4, tag_with_include.taggings.length + assert_equal 5, tag_with_include.taggings.length end def test_has_many_through_has_many_find_all @@ -589,6 +589,12 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase assert taggables.include?(posts(:welcome)) end + def test_preload_nil_polymorphic_belongs_to + assert_nothing_raised do + taggings = Tagging.find(:all, :include => :taggable, :conditions => ['taggable_type IS NULL']) + end + end + def test_preload_polymorphic_has_many posts = Post.find(:all, :order => 'posts.id') posts_with_taggings = Post.find(:all, :include => :taggings, :order => 'posts.id') diff --git a/activerecord/test/fixtures/taggings.yml b/activerecord/test/fixtures/taggings.yml index 2213b15494..1e3d5965b8 100644 --- a/activerecord/test/fixtures/taggings.yml +++ b/activerecord/test/fixtures/taggings.yml @@ -22,4 +22,7 @@ godfather: tag_id: 1 taggable_id: 1 taggable_type: Item -
\ No newline at end of file + +orphaned: + id: 5 + tag_id: 1 |