Fix hm:t to self table aliasing in construct_scope

This commit is contained in:
Ernie Miller
2010-05-03 20:47:42 -04:00
committed by Vijay Dev
parent 7bffa9dd01
commit 5d37ff6bb2
3 changed files with 12 additions and 3 deletions

View File

@@ -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 = []

View File

@@ -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

View File

@@ -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' }