This project is archived and is in readonly mode.
AR Association Proxy prevents calling methods defined using method_missing on the target of the association
Reported by Karl Varga | November 11th, 2009 @ 08:26 AM
From the commit:
Don't raise a NoMethodError if the target does not respond_to? method. This excludes methods on target that are implemented using method_missing. And if the method really doesn't exist, target.send(method, args) will raise a NoMethodError itself...so let it do that.
Description:
If I have a model like this:
class User < ActiveRecord::Base
# Provide is_admin?, is_guest? account type discovery methods def
method_missing(method_id, *arguments)
if match = /^is_(\w+)\?$/.match(method_id.to_s)
self.account_type == match[1]
else
super
end
end end
class Activity < ActiveRecord::Base
belongs_to :user end
I can't do Activity.first.user.is_guest? because it raises a NoMethodError. The problem is that the Association Proxy only allows calling methods on the target that respond_to?(method). It doesn't need to do this check, because if the method really doesn't exist, the call to target.send(method, args) will raise NoMethodError itself, so nothing is gained by doing that check in the proxy.
Comments and changes to this ticket
-
Michael Koziarski November 19th, 2009 @ 03:24 AM
- State changed from new to invalid
If you override method_missing you must also override respond_to?
The check is there to prevent calling private methods.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>