From 1b773bca5c77d99d565d86f7da0f05e31cbb03ce Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Tue, 12 May 2020 11:10:39 -0700 Subject: [PATCH] Avoid confliting Kernel-named scopes on Relation A previous change made singleton methods eagerly define their relation methods if it shared a name with a method on Kernel. This caused issues with a few methods which were both defined on Kernel and on AcitveRecord::Relation. This commit avoids defining the method if it exists on AR::Relation. --- activerecord/lib/active_record/scoping/named.rb | 2 +- activerecord/test/models/reply.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index 46cfb2ea21..d1bf4bdead 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -199,7 +199,7 @@ def scope(name, body, &block) private def singleton_method_added(name) - generate_relation_method(name) if Kernel.respond_to?(name) + generate_relation_method(name) if Kernel.respond_to?(name) && !ActiveRecord::Relation.method_defined?(name) end def valid_scope_name?(name) diff --git a/activerecord/test/models/reply.rb b/activerecord/test/models/reply.rb index 074a51f3df..49536a02e1 100644 --- a/activerecord/test/models/reply.rb +++ b/activerecord/test/models/reply.rb @@ -10,9 +10,14 @@ class Reply < Topic scope :ordered, -> { Reply.order(:id) } + # Method on Kernel def self.open approved end + + # Methods both on Kernel and Relation + def self.load(data:); end + def self.select(data:); end end class SillyReply < Topic -- GitLab