From ea6139101ccaf8be03b536b1293a9f36bc12f2f7 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Tue, 25 Jul 2017 14:22:16 -0400 Subject: Allow `Relation#or` to accept a relation with different `references` Note that the two relations must still have the same `includes` values (which is the only time `references` actually does anything). It makes sense for us to allow this, as `references` is called implicitly when passing a hash to `where`. Fixes #29411 --- activerecord/CHANGELOG.md | 7 +++++++ activerecord/lib/active_record/relation/query_methods.rb | 3 ++- activerecord/test/cases/relation/or_test.rb | 11 +++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index a656c767c0..1ff7010c2f 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,10 @@ +* `Relation#or` now accepts two relations who have different values for + `references` only, as `references` can be implicitly called by `where`. + + Fixes #29411. + + *Sean Griffin* + * ApplicationRecord is no longer generated when generating models. If you need to generate it, it can be created with `rails g application_record`. diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index a51cf4319e..f7fe968b55 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -635,6 +635,7 @@ module ActiveRecord self.where_clause = self.where_clause.or(other.where_clause) self.having_clause = having_clause.or(other.having_clause) + self.references_values += other.references_values self end @@ -1158,7 +1159,7 @@ module ActiveRecord end end - STRUCTURAL_OR_METHODS = Relation::VALUE_METHODS - [:extending, :where, :having, :unscope] + STRUCTURAL_OR_METHODS = Relation::VALUE_METHODS - [:extending, :where, :having, :unscope, :references] def structurally_incompatible_values_for_or(other) STRUCTURAL_OR_METHODS.reject do |method| get_value(method) == other.get_value(method) diff --git a/activerecord/test/cases/relation/or_test.rb b/activerecord/test/cases/relation/or_test.rb index 3269620987..b01801b41f 100644 --- a/activerecord/test/cases/relation/or_test.rb +++ b/activerecord/test/cases/relation/or_test.rb @@ -1,11 +1,14 @@ # frozen_string_literal: true require "cases/helper" +require "models/author" +require "models/categorization" require "models/post" module ActiveRecord class OrTest < ActiveRecord::TestCase fixtures :posts + fixtures :authors def test_or_with_relation expected = Post.where("id = 1 or id = 2").to_a @@ -115,5 +118,13 @@ module ActiveRecord Post.where(id: [1, 2, 3]).or(title: "Rails") end end + + def test_or_with_references_inequality + joined = Post.includes(:author) + actual = joined.where(authors: { id: 1 }) + .or(joined.where(title: "I don't have any comments")) + expected = Author.find(1).posts + Post.where(title: "I don't have any comments") + assert_equal expected.sort_by(&:id), actual.sort_by(&:id) + end end end -- cgit v1.2.3