Browse files

This patch ensures that an inner join performed to a polymorphic reso…

…urce joins on both the polymorphic_id and polymorphic_type fields.

Previously, the inner joins were merely joining on the polymorphic_id field.
  • Loading branch information...
1 parent c751d84 commit b538e8550b9e369d774086376d2243cebe9c1a6d @daphonz daphonz committed Apr 29, 2011
View
2 activerecord/lib/active_record/associations.rb
@@ -2199,7 +2199,7 @@ def association_join
[
[parent_table[jt_primary_key].eq(join_table[jt_foreign_key]), jt_as_extra, jt_source_extra, jt_sti_extra].reject{|x| x.blank? },
- aliased_table[first_key].eq(join_table[second_key])
+ [aliased_table[first_key].eq(join_table[second_key]), as_extra].reject{ |x| x.blank? }
]
elsif reflection.options[:as]
id_rel = aliased_table["#{reflection.options[:as]}_id"].eq(parent_table[parent.primary_key])
View
9 activerecord/test/cases/associations/inner_join_association_test.rb
@@ -4,6 +4,7 @@
require 'models/author'
require 'models/category'
require 'models/categorization'
+require 'models/tagging'
class InnerJoinAssociationTest < ActiveRecord::TestCase
fixtures :authors, :posts, :comments, :categories, :categories_posts, :categorizations
@@ -23,6 +24,14 @@ def test_construct_finder_sql_ignores_empty_joins_array
assert_no_match(/JOIN/i, sql)
end
+ def test_construct_finder_sql_on_polymorphic_through_includes_type
+ # Posts has one tagging (polymorphic)
+ # Author has one tagging through post.
+ # Should be something like:
+ # INNER JOIN taggings.taggable_id = posts.id AND taggings.taggable_type = 'Post'
+ assert_match(/taggable_type/i, Author.joins(:taggings).to_sql.to_s)
+ end
+
def test_find_with_implicit_inner_joins_honors_readonly_without_select
authors = Author.joins(:posts).to_a
assert !authors.empty?, "expected authors to be non-empty"

0 comments on commit b538e85

Please sign in to comment.