diff options
author | Ernie Miller <ernie@metautonomo.us> | 2010-05-03 20:47:42 -0400 |
---|---|---|
committer | Vijay Dev <vijaydev.cse@gmail.com> | 2010-12-16 01:49:29 +0530 |
commit | 5d37ff6bb2b1f948034bf06e450f2fdb8195c4e5 (patch) | |
tree | 4f051cd12065f9799f4b5c1016854ee372883de9 | |
parent | 7bffa9dd01cdef3088e0d33fabd5fc07eb35c9d9 (diff) | |
download | rails-5d37ff6bb2b1f948034bf06e450f2fdb8195c4e5.tar.gz rails-5d37ff6bb2b1f948034bf06e450f2fdb8195c4e5.tar.bz2 rails-5d37ff6bb2b1f948034bf06e450f2fdb8195c4e5.zip |
Fix hm:t to self table aliasing in construct_scope
3 files changed, 12 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/through_association_scope.rb b/activerecord/lib/active_record/associations/through_association_scope.rb index 6b7faa9fc3..a55bf6323e 100644 --- a/activerecord/lib/active_record/associations/through_association_scope.rb +++ b/activerecord/lib/active_record/associations/through_association_scope.rb @@ -21,9 +21,17 @@ module ActiveRecord construct_owner_attributes(@reflection) end + def aliased_through_table + name = @reflection.through_reflection.table_name + + @reflection.table_name == name ? + @reflection.through_reflection.klass.arel_table.alias(name + "_join") : + @reflection.through_reflection.klass.arel_table + end + # Build SQL conditions from attributes, qualified by table name. def construct_conditions - table = @reflection.through_reflection.klass.arel_table + table = aliased_through_table conditions = construct_quoted_owner_attributes(@reflection.through_reflection).map do |attr, value| table[attr].eq(value) end @@ -53,7 +61,7 @@ module ActiveRecord end def construct_joins - right = @reflection.through_reflection.klass.arel_table + right = aliased_through_table left = @reflection.klass.arel_table conditions = [] diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index fe60e676d6..34ae297275 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -392,7 +392,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase sarah = Person.create!(:first_name => 'Sarah', :primary_contact_id => people(:susan).id, :gender => 'F', :number1_fan_id => 1) john = Person.create!(:first_name => 'John', :primary_contact_id => sarah.id, :gender => 'M', :number1_fan_id => 1) assert_equal sarah.agents, [john] - assert_equal people(:susan).agents_of_agents, [john] + assert_equal people(:susan).agents.map(&:agents).flatten, people(:susan).agents_of_agents end def test_collection_singular_ids_getter_with_string_primary_keys diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb index 951ec93c53..bee89de042 100644 --- a/activerecord/test/models/person.rb +++ b/activerecord/test/models/person.rb @@ -12,6 +12,7 @@ class Person < ActiveRecord::Base belongs_to :primary_contact, :class_name => 'Person' has_many :agents, :class_name => 'Person', :foreign_key => 'primary_contact_id' + has_many :agents_of_agents, :through => :agents, :source => :agents belongs_to :number1_fan, :class_name => 'Person' scope :males, :conditions => { :gender => 'M' } |