Browse files

Fix instance_eval calls to association proxies

In the current stable, ActiveRecord::Associations::AssociationProxy#method_missing calls yield() if a block is given, causing the block to always be evaluated in its calling context. However, in the case of instance_eval, correct behavior requires that the block be passed directly to the @target, rather than being evaluated inside a different block. Incidentally, this also simplifies the code slightly.

[#3412 state:committed]

Signed-off-by: Jeremy Kemper <>
  • Loading branch information...
1 parent 50c28e7 commit 49e943c4f0ac3459bd53023167aaa08fc8e46733 Mat Brown committed with jeremy Oct 22, 2009
@@ -200,18 +200,14 @@ def with_scope(*args, &block)
# Forwards any missing method call to the \target.
- def method_missing(method, *args)
+ def method_missing(method, *args, &block)
if load_target
unless @target.respond_to?(method)
message = "undefined method `#{method.to_s}' for \"#{@target}\":#{@target.class.to_s}"
raise NoMethodError, message
- if block_given?
- @target.send(method, *args) { |*block_args| yield(*block_args) }
- else
- @target.send(method, *args)
- end
+ @target.send(method, *args, &block)
@@ -1178,5 +1178,13 @@ def test_creating_using_primary_key
client = firm.clients_using_primary_key.create!(:name => 'test')
assert_equal, client.firm_name
+ def test_normal_method_call_in_association_proxy
+ assert_equal 'Welcome to the weblog', { |comment| }.first.title
+ end
+ def test_instance_eval_in_association_proxy
+ assert_equal 'Welcome to the weblog', { |comment| }.first.instance_eval{title}
+ end

0 comments on commit 49e943c

Please sign in to comment.