aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/through_association_scope.rb
diff options
context:
space:
mode:
authorErnie Miller <ernie@metautonomo.us>2010-05-03 20:47:42 -0400
committerAaron Patterson <aaron.patterson@gmail.com>2010-12-14 16:16:34 -0800
commitc81af00a7ab64ec1159e8b319f8498ea5b9037c7 (patch)
tree94d7c48d117643418c402c4226a97aa942d3e10b /activerecord/lib/active_record/associations/through_association_scope.rb
parent00c893d3b86be3a141c21ed065e085adbb26062a (diff)
downloadrails-c81af00a7ab64ec1159e8b319f8498ea5b9037c7.tar.gz
rails-c81af00a7ab64ec1159e8b319f8498ea5b9037c7.tar.bz2
rails-c81af00a7ab64ec1159e8b319f8498ea5b9037c7.zip
Fix hm:t to self table aliasing in construct_scope
Diffstat (limited to 'activerecord/lib/active_record/associations/through_association_scope.rb')
-rw-r--r--activerecord/lib/active_record/associations/through_association_scope.rb12
1 files changed, 10 insertions, 2 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 = []