Reflection on association / AssociationProxy

When the object referenced by the association exists, it's a real
AssociationProxy, and all the methods work:
http://apidock.com/rails/ActiveRecord/Associations/AssociationProxy

Container.first.pickup_at.class.to_s

=> "Terminal"

Container.first.pickup_at.aliased_table_name

=> "companies"

When the object is new:

Container.new.pickup_at.class

=> NilClass

ImportContainer.new.pickup_at.aliased_table_name

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?

Constantin,

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.