Fix joins that reserved word association is referenced in where

It is caused by #40749.

`Arel.sql`ed string will be avoided from quoting, it should be raw
string before using it for table alias.

Fixes https://github.com/rails/rails/issues/41010#issuecomment-761994291.
This commit is contained in:
Ryuta Kamizono 2021-01-18 16:17:28 +09:00
parent e889cc55b0
commit 00227ff3ee
5 changed files with 11 additions and 2 deletions

@ -195,7 +195,7 @@ def make_constraints(parent, child, join_type)
next table, true
end
table_name = @references[reflection.name.to_sym]
table_name = @references[reflection.name.to_sym]&.to_s
table = alias_tracker.aliased_table_for(reflection.klass.arel_table, table_name) do
name = reflection.alias_candidate(parent.table_name)

@ -122,6 +122,11 @@ def test_join_conditions_allow_nil_associations
assert_equal 1, authors.count
end
def test_join_with_reserved_word
assert_equal [categories_posts(:technology_welcome)],
Post::CategoryPost.joins(:group).where("group.id": categories(:technology))
end
def test_find_with_implicit_inner_joins_without_select_does_not_imply_readonly
authors = Author.joins(:posts)
assert_not authors.empty?, "expected authors to be non-empty"

@ -1,3 +1,6 @@
_fixture:
model_class: Post::CategoryPost
general_welcome:
category_id: 1
post_id: 1

@ -3,6 +3,7 @@
class Post < ActiveRecord::Base
class CategoryPost < ActiveRecord::Base
self.table_name = "categories_posts"
belongs_to :group, foreign_key: :category_id, class_name: "Category"
belongs_to :category
belongs_to :post
end

@ -169,7 +169,7 @@
t.integer :categorizations_count
end
create_table :categories_posts, force: true, id: false do |t|
create_table :categories_posts, force: true do |t|
t.integer :category_id, null: false
t.integer :post_id, null: false
end