ActiveRecord AssociationProxy magic?

say you have User, User has_many :addresses. User.addresses is not an array. It looks like an array, it quacks like an array but it's actually an association proxy pretending to be an array. It manages an actual array with the elements of the collection and (via method_missing) lets that array handle most calls.


because the class method is also proxyed through to the target array, which returns Array because it is one.


when you say the class method, you mean like has_many, belongs_to,
etc? Can you point to me in the code where this is done. Where lines of
code are responsible for the "proxy through to the target array" part.

No I mean the method "class", which you call to get the class of an
object. The code you are interested in is in association_proxy.rb

Ok, Im looking in association_proxy.rb and I can't find where they overwrite the "class" method to return Array.

Should I be looking in a different file?

Nope. The point is that it's not overwriting anything: it's (at the
top) undefining just about every method (including class) and then, in
method_missing it forwards on the method to the target