Avoid leaking the first relation we call #first on

With the previous implementation, the block passed to
define_singleton_method, which will live forever as the method body,
captures the parameters (args and block) in its enclosure.

For the current_scope registry, that can include an AR::Relation.
This commit is contained in:
Matthew Draper 2015-10-09 06:59:51 +10:30
parent 2f5ceffdfa
commit 1b6fcae948
2 changed files with 9 additions and 3 deletions

@ -1,3 +1,9 @@
* Avoid leaking the first relation we call `first` on, per model.
Fixes #21921.
*Matthew Draper*, *Jean Boussier*
* Remove unused `pk_and_sequence_for` in AbstractMysqlAdapter.
*Ryuta Kamizono*

@ -43,9 +43,9 @@ def instance
protected
def method_missing(name, *args, &block) # :nodoc:
# Caches the method definition as a singleton method of the receiver.
define_singleton_method(name) do |*a, &b|
instance.public_send(name, *a, &b)
end
#
# By letting #delegate handle it, we avoid an enclosure that'll capture args.
singleton_class.delegate name, to: :instance
send(name, *args, &block)
end