mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Fix hm:t to self table aliasing in construct_scope
This commit is contained in:
@@ -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 = []
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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' }
|
||||
|
||||
Reference in New Issue
Block a user