Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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 <jeremy@bitsweat.net>
  • Loading branch information...
commit 2f1ded306728ce8ff614e09a113398328e7fe6ca 1 parent 1db3a27
Mat Brown authored jeremy committed
8 activerecord/lib/active_record/associations/association_proxy.rb
View
@@ -208,14 +208,10 @@ def with_scope(*args, &block)
private
# Forwards any missing method call to the \target.
- def method_missing(method, *args)
+ def method_missing(method, *args, &block)
if load_target
if @target.respond_to?(method)
- if block_given?
- @target.send(method, *args) { |*block_args| yield(*block_args) }
- else
- @target.send(method, *args)
- end
+ @target.send(method, *args, &block)
else
super
end
8 activerecord/test/cases/associations/has_many_associations_test.rb
View
@@ -1130,5 +1130,13 @@ def test_creating_using_primary_key
client = firm.clients_using_primary_key.create!(:name => 'test')
assert_equal firm.name, client.firm_name
end
+
+ def test_normal_method_call_in_association_proxy
+ assert_equal 'Welcome to the weblog', Comment.all.map { |comment| comment.post }.first.title
+ end
+
+ def test_instance_eval_in_association_proxy
+ assert_equal 'Welcome to the weblog', Comment.all.map { |comment| comment.post }.first.instance_eval{title}
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.