Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix hm:t to self table aliasing in construct_scope

  • Loading branch information...
commit 5d37ff6bb2b1f948034bf06e450f2fdb8195c4e5 1 parent 7bffa9d
@ernie ernie authored vijaydev committed
View
12 activerecord/lib/active_record/associations/through_association_scope.rb
@@ -21,9 +21,17 @@ def construct_create_scope
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 @@ def construct_select(custom_select = nil)
end
def construct_joins
- right = @reflection.through_reflection.klass.arel_table
+ right = aliased_through_table
left = @reflection.klass.arel_table
conditions = []
View
2  activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -392,7 +392,7 @@ def test_has_many_association_through_a_has_many_association_to_self
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
View
1  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' }

0 comments on commit 5d37ff6

Please sign in to comment.
Something went wrong with that request. Please try again.