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 6a7e92dc28806fa2e7b61a1a1c1b27f40312b43a..3615e0c6c3f179f19f29222bcf0efcb619683722 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -37,7 +37,7 @@ def join_constraints(foreign_table, foreign_klass, join_type, alias_tracker) nodes = arel.constraints.first others = nodes.children.extract! do |node| - Arel.fetch_attribute(node) { |attr| attr.relation.name != table.name } + !Arel.fetch_attribute(node) { |attr| attr.relation.name == table.name } end joins << table.create_join(table, table.create_on(nodes), join_type) diff --git a/activerecord/test/models/rating.rb b/activerecord/test/models/rating.rb index 2a18ea45aca789043213c5eb3cf3b36b770aa387..c117a677e0981a9903dcc519e2acd93e06449ae4 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