# Reflection on association / AssociationProxy

**URL:** <https://www.ruby-forum.com/t/reflection-on-association-associationproxy/147753>\
**Category:** Rails\
**Created:** [September 5, 2008, 3:23am UTC](https://www.ruby-forum.com/t/reflection-on-association-associationproxy/147753 "2008-09-05T03:23:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [September 5, 2008, 3:23am UTC](https://www.ruby-forum.com/t/reflection-on-association-associationproxy/147753/1 "2008-09-05T03:23:50Z")

</div>

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](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?

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [September 5, 2008, 5:27am UTC](https://www.ruby-forum.com/t/reflection-on-association-associationproxy/147753/2 "2008-09-05T05:27:05Z")

</div>

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.

On Sep 4, 8:23 pm, Constantin G. \<rails-mailing-l…@andreas-
