Skip to content

Commit

Permalink
Fix joins that reserved word association is referenced in where
Browse files Browse the repository at this point in the history
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 #41010 (comment).
  • Loading branch information
kamipo committed Jan 18, 2021
1 parent e889cc5 commit 00227ff
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 2 deletions.
Expand Up @@ -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)
Expand Down
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions activerecord/test/fixtures/categories_posts.yml
@@ -1,3 +1,6 @@
_fixture:
model_class: Post::CategoryPost

general_welcome:
category_id: 1
post_id: 1
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/post.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/schema/schema.rb
Expand Up @@ -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
Expand Down

0 comments on commit 00227ff

Please sign in to comment.