This project is archived and is in readonly mode.
Association proxy does not forward missing methods to the actual instance
Reported by wereHamster | November 13th, 2010 @ 01:00 PM
Given a simple association:
class City < ActiveRecord::Base
belongs_to :country
end
class Country < ActiveRecord::Base
def method_missing(method, *args)
p "method #{method} not found on #{self}"
end
end
I am able to create objects of type Country and missing methods are correctly forwarded to my method_missing:
country = Country.new
country.non_existing_method
# prints "method non_existing_method not found on #<Country..>"
However when I create an City object and assign the country object, it stops working:
city = City.new(:country => country)
city.country.non_existing_method
# raises NoMethodError
I can work around this by using try:
city.country.try(:non_existing_method)
But that is ugly. And not intuitive.
Comments and changes to this ticket
-
wereHamster November 13th, 2010 @ 01:20 PM
Just when I posted the ticket I figured out how the association proxies work. How does this patch look?
-
wereHamster November 13th, 2010 @ 01:25 PM
Bah, lighthouse won't let me attach the patch.. see here instead: https://github.com/wereHamster/rails/compare/master...lighthouse/59...
-
Neeraj Singh November 13th, 2010 @ 05:50 PM
- Importance changed from to Low
Patch looks good. I think you should attach a test too.
-
Cesario November 13th, 2010 @ 08:21 PM
- Tag set to active record, 3.0
Hi,
Your commit makes the tests go read.
The problem with defining method with method_missing is that
@target.respond_to?(method)
(in the association proxy piece of code you mention) will always returnsfalse
. -
rails February 14th, 2011 @ 12:00 AM
- State changed from new to open
- Tag changed from active record, 3.0 to active record, 30
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
rails February 14th, 2011 @ 12:00 AM
- State changed from open to stale
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>