From 9684bd2e87d35099c9b28f545d1e00c0971193d7 Mon Sep 17 00:00:00 2001 From: Mat Brown Date: Wed, 2 Dec 2009 16:16:38 -0500 Subject: [PATCH] Only pass named block directly in AssociationProxy#instance_eval This fixes the bug with handling of #instance_eval while maintaining the performance benefits of yielding from a block in the general case. --- .../associations/association_proxy.rb | 17 +++++++++++++++-- 1 files changed, 15 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index e1fce9a..8c31141 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -149,6 +149,15 @@ module ActiveRecord end end + # The optimized method_missing implementation does not properly handle + # this method, in which the block needs to be delegated directly + # to the target. + def instance_eval(&block) + if load_target + @target.instance_eval(&block) + end + end + protected # Does the association have a :dependent option? def dependent? @@ -208,10 +217,14 @@ module ActiveRecord private # Forwards any missing method call to the \target. - def method_missing(method, *args, &block) + def method_missing(method, *args) if load_target if @target.respond_to?(method) - @target.send(method, *args, &block) + if block_given? + @target.send(method, *args) { |*block_args| yield(*block_args) } + else + @target.send(method, *args) + end else super end -- 1.6.3.3