diff options
author | Rick Olson <technoweenie@gmail.com> | 2006-04-05 17:11:21 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2006-04-05 17:11:21 +0000 |
commit | 58c435a3caebbe31c02e80c8f32840f0daa57d68 (patch) | |
tree | ffecd5a19ee67a1bc70223141955006a3f8bac61 | |
parent | a9fb1544b0e7e6412e4cb64702585024e24276bf (diff) | |
download | rails-58c435a3caebbe31c02e80c8f32840f0daa57d68.tar.gz rails-58c435a3caebbe31c02e80c8f32840f0daa57d68.tar.bz2 rails-58c435a3caebbe31c02e80c8f32840f0daa57d68.zip |
Support eager includes when going through a polymorphic has_many association. [Rick]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4171 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 50 | ||||
-rw-r--r-- | activerecord/test/associations_join_model_test.rb | 2 |
3 files changed, 35 insertions, 19 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index bd06810c36..9e3dd9d735 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Support eager includes when going through a polymorphic has_many association. [Rick] + * Added support for eagerly including polymorphic has_one associations. (closes #4525) [Rick] class Post < ActiveRecord::Base diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 7b6257ec03..8b6aac49ce 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1445,24 +1445,38 @@ module ActiveRecord aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key ] else - case source_reflection.macro - when :belongs_to - first_key = primary_key - second_key = options[:foreign_key] || klass.to_s.classify.foreign_key - when :has_many - first_key = through_reflection.klass.to_s.classify.foreign_key - second_key = options[:foreign_key] || primary_key + if source_reflection.macro == :has_many && source_reflection.options[:as] + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ + table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, + through_reflection.primary_key_name, + parent.aliased_table_name, parent.primary_key] + + " LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s " % [ + table_name_and_alias, + aliased_table_name, "#{source_reflection.options[:as]}_id", + aliased_join_table_name, options[:foreign_key] || primary_key, + aliased_table_name, "#{source_reflection.options[:as]}_type", + klass.quote(source_reflection.active_record.base_class.name) + ] + else + case source_reflection.macro + when :belongs_to + first_key = primary_key + second_key = options[:foreign_key] || klass.to_s.classify.foreign_key + when :has_many + first_key = through_reflection.klass.to_s.classify.foreign_key + second_key = options[:foreign_key] || primary_key + end + + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ + table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, + through_reflection.primary_key_name, + parent.aliased_table_name, parent.primary_key] + + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ + table_name_and_alias, + aliased_table_name, first_key, + aliased_join_table_name, second_key + ] end - - " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ - table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, - through_reflection.primary_key_name, - parent.aliased_table_name, parent.primary_key] + - " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ - table_name_and_alias, - aliased_table_name, first_key, - aliased_join_table_name, second_key - ] end when reflection.macro == :has_many && reflection.options[:as] @@ -1479,7 +1493,7 @@ module ActiveRecord aliased_table_name, "#{reflection.options[:as]}_id", parent.aliased_table_name, parent.primary_key, aliased_table_name, "#{reflection.options[:as]}_type", - klass.quote(reflection.active_record.name) + klass.quote(reflection.active_record.base_class.name) ] else foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb index 4a11d4c5eb..93cfd0084c 100644 --- a/activerecord/test/associations_join_model_test.rb +++ b/activerecord/test/associations_join_model_test.rb @@ -311,7 +311,7 @@ class AssociationsJoinModelTest < Test::Unit::TestCase assert_equal [taggings(:welcome_general), taggings(:thinking_general)], authors(:david).taggings.uniq.sort_by { |t| t.id } end - def test_has_many_through_polymorphic_has_many_with_eager_loading + def test_include_has_many_through_polymorphic_has_many author = Author.find_by_id(authors(:david).id, :include => :taggings) expected_taggings = [taggings(:welcome_general), taggings(:thinking_general)] assert_no_queries do |