Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support eager includes when going through a polymorphic has_many asso…

…ciation. [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4171 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 58c435a3caebbe31c02e80c8f32840f0daa57d68 1 parent a9fb154
@technoweenie technoweenie authored
View
2  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
View
50 activerecord/lib/active_record/associations.rb
@@ -1445,24 +1445,38 @@ def association_join
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 @@ def association_join
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
View
2  activerecord/test/associations_join_model_test.rb
@@ -311,7 +311,7 @@ def test_has_many_through_polymorphic_has_many
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
Please sign in to comment.
Something went wrong with that request. Please try again.