1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Ensure associations doesn't table name collide with aliased joins

Currently alias tracker only refer a table name, doesn't respect an
alias name. Should use `join.left.name` rather than
`join.left.table_name`.
This commit is contained in:
Ryuta Kamizono 2017-10-24 21:47:11 +09:00
parent d147ed02a0
commit 019583e939
2 changed files with 9 additions and 1 deletions

View file

@ -31,7 +31,7 @@ module ActiveRecord
/JOIN(?:\s+\w+)?\s+(?:\S+\s+)?(?:#{quoted_name}|#{name})\sON/i
).size
elsif join.respond_to? :left
join.left.table_name == name ? 1 : 0
join.left.name == name ? 1 : 0
elsif join.is_a?(Hash)
join[name]
else

View file

@ -37,6 +37,14 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase
assert_match(/agents_people_2/i, sql)
end
def test_construct_finder_sql_does_not_table_name_collide_with_aliased_joins
people = Person.arel_table
agents = people.alias("agents_people")
constraint = agents[:primary_contact_id].eq(people[:id])
sql = Person.joins(:agents).joins(agents.create_join(agents, agents.create_on(constraint))).to_sql
assert_match(/agents_people_2/i, sql)
end
def test_construct_finder_sql_ignores_empty_joins_hash
sql = Author.joins({}).to_sql
assert_no_match(/JOIN/i, sql)