Skip to content

Commit 90946da

Browse files
committed
Avoid eagerly generating Module delegation methods
We special case singleton methods being added to ActiveRecord::Base subclasses whose names match methods on Kernel so that we can eagerly define them on the ClassSpecificRelation as otherwise we'd never hit method_missing. Previously, this would also eagerly define methods which were on the Kernel Module's singleton class rather than being defined as instance methods inside the module. Most notably this hit "name" which is defined on the anonymous HABTM classes.
1 parent d1cefb4 commit 90946da

File tree

1 file changed

+4
-1
lines changed
  • activerecord/lib/active_record/scoping

1 file changed

+4
-1
lines changed

activerecord/lib/active_record/scoping/named.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ def scope(name, body, &block)
190190

191191
private
192192
def singleton_method_added(name)
193-
generate_relation_method(name) if Kernel.respond_to?(name) && !ActiveRecord::Relation.method_defined?(name)
193+
# Most Kernel extends are both singleton and instance methods so
194+
# respond_to is a fast check, but we don't want to define methods
195+
# only on the module (ex. Module#name)
196+
generate_relation_method(name) if Kernel.respond_to?(name) && (Kernel.instance_methods | Kernel.private_instance_methods).include?(name) && !ActiveRecord::Relation.method_defined?(name)
194197
end
195198
end
196199
end

0 commit comments

Comments
 (0)