Reflection on association / AssociationProxy

When the object referenced by the association exists, it's a real AssociationProxy, and all the methods work:


=> "Terminal"


=> "companies"

When the object is new:

=> NilClass

NoMethodError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.aliased_table_name

It seems to be a real nil object, and not a AssociationProxy with the "class" method removed.

The question is: How do I reflect on the association when the object is new?


You can't. When you create a new Container object, it doesn't have an associated object yet, is completely empty, but you have a Nil object on your Container company_id attribute. (I'm guessing the relationship is being named pickup_at). That's what you are getting.

The AssociationProxy returns the target object methods only if it exists.

I can't think of a way to reflect on an inexistent referenced object.

Regards, Jorge Corona.