# Can only traverse a polymorphic association in one direction

**URL:** <https://www.ruby-forum.com/t/can-only-traverse-a-polymorphic-association-in-one-direction/189247>\
**Category:** Rails\
**Created:** [May 27, 2010, 1:19pm UTC](https://www.ruby-forum.com/t/can-only-traverse-a-polymorphic-association-in-one-direction/189247 "2010-05-27T13:19:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BarryOg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/91b2a8/32.png) [@BarryOg](https://www.ruby-forum.com/u/BarryOg)\
**Post date:** [May 27, 2010, 1:19pm UTC](https://www.ruby-forum.com/t/can-only-traverse-a-polymorphic-association-in-one-direction/189247/1 "2010-05-27T13:19:51Z")

</div>

Hi all, I’m working on a polymorphic association and I’ve run into a bit  
of a  
stumbling block. I only seem to be able to traverse the relationship in  
one  
direction and not the other. This is what my code looks like:

class NodeEntry \< ActiveRecord::Base  
acts\_as\_nested\_set  
belongs\_to :node, :polymorphic =\> true  
end

class Location \< ActiveRecord::Base  
attr\_accessor :name  
has\_one :node\_entry, :as =\> :node  
validates\_presence\_of :country, :region, :name  
end

class Meter \< ActiveRecord::Base  
attr\_accessor :name, :parent\_id  
has\_one :node\_entry, :as =\> :node  
validates\_presence\_of :meter\_identifier, :type\_of\_meter  
end

class MeterGroup \< ActiveRecord::Base  
has\_one :node\_entry, :as =\> :node  
end

When I create a new location or meter I use the following code:  
@location = Location.new(params[:location])  
@location\_node = NodeEntry.new(:name =\> params[:location][:name])  
@location\_node.node = @location  
@location\_node.save

After loading a location or meter object from the database, when I  
attempt  
to do a location.node I get an error saying the object doesn’t have that  
method. When I load a node entry object I have no problem accessing its  
node and I get the correct class back.

I can get around this by doing a simple query such as:  
NodeEntry.first(:conditions =\> “node\_id = #{loc.id} and node\_type =  
‘Location’”).name

but I had assumed active record should be doing this for me under the  
covers. Have I made a mistake somewhere or is this the expected  
behavior of  
polymorphic associations?

–  
View this message in context:  
[http://old.nabble.com/Can-only-traverse-a-polymorphic-association-in-one-direction-tp28692572p28692572.html](http://old.nabble.com/Can-only-traverse-a-polymorphic-association-in-one-direction-tp28692572p28692572.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![BarryOg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/91b2a8/32.png) [@BarryOg](https://www.ruby-forum.com/u/BarryOg)\
**Post date:** [May 29, 2010, 11:47pm UTC](https://www.ruby-forum.com/t/can-only-traverse-a-polymorphic-association-in-one-direction/189247/2 "2010-05-29T23:47:13Z")

</div>

On May 27, 7:18 am, BarryOg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> attr_accessor :name
> 
> ```
> 
> class MeterGroup \< ActiveRecord::Base  
> to do a location.node I get an error saying the object doesn’t have that  
> method.

The has\_one is still named what you’ve specified (:node\_entry in this  
case), the polymorphic stuff doesn’t change that…

So it’d be:

@location.node\_entry

not

@location.node

–Matt J.

---

<div class="post-metadata">

**Author:** ![BarryOg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/91b2a8/32.png) [@BarryOg](https://www.ruby-forum.com/u/BarryOg)\
**Post date:** [June 3, 2010, 11:52am UTC](https://www.ruby-forum.com/t/can-only-traverse-a-polymorphic-association-in-one-direction/189247/3 "2010-06-03T11:52:33Z")

</div>

Thanks for taking the time to read through my code and spot the obvious,  
I  
guess too much coffee and too little sleep is not the best recipe.

Matt J. wrote:

> removed\_email\_address@domain.invalid.  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
View this message in context:  
[http://old.nabble.com/Can-only-traverse-a-polymorphic-association-in-one-direction-tp28692572p28765373.html](http://old.nabble.com/Can-only-traverse-a-polymorphic-association-in-one-direction-tp28692572p28765373.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).
