From 1cea9741b9b5b8f84ee7abce6e13e0fcf8ae06d0 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Fri, 4 Oct 2019 15:46:39 +0900 Subject: [PATCH] Merge pull request #37359 from kamipo/maintain_extra_joins Maintain extra joins for string or complex arel conditions --- .../associations/join_dependency/join_association.rb | 2 +- activerecord/test/models/rating.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb index 84ee7eed70..4c4ec5fc6e 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -36,7 +36,7 @@ def join_constraints(foreign_table, foreign_klass, join_type, alias_tracker) nodes = arel.constraints.first others, children = nodes.children.partition do |node| - fetch_arel_attribute(node) { |attr| attr.relation.name != table.name } + !fetch_arel_attribute(node) { |attr| attr.relation.name == table.name } end nodes = table.create_and(children) diff --git a/activerecord/test/models/rating.rb b/activerecord/test/models/rating.rb index 2a18ea45ac..c117a677e0 100644 --- a/activerecord/test/models/rating.rb +++ b/activerecord/test/models/rating.rb @@ -3,6 +3,6 @@ class Rating < ActiveRecord::Base belongs_to :comment has_many :taggings, as: :taggable - has_many :taggings_without_tag, -> { left_joins(:tag).where("tags.id": nil) }, as: :taggable, class_name: "Tagging" + has_many :taggings_without_tag, -> { left_joins(:tag).where("tags.id": [nil, 0]) }, as: :taggable, class_name: "Tagging" has_many :taggings_with_no_tag, -> { joins("LEFT OUTER JOIN tags ON tags.id = taggings.tag_id").where("tags.id": nil) }, as: :taggable, class_name: "Tagging" end -- GitLab