# To\_json and polymorphic models

**URL:** <https://www.ruby-forum.com/t/to-json-and-polymorphic-models/108778>\
**Category:** Rails\
**Created:** [July 6, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/to-json-and-polymorphic-models/108778 "2007-07-06T22:32:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jif](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eb8c5e/32.png) [@Jif](https://www.ruby-forum.com/u/Jif)\
**Post date:** [July 6, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/to-json-and-polymorphic-models/108778/1 "2007-07-06T22:32:38Z")

</div>

I have tried to use to\_json to create an array that will be  
manipulated in javascript on the client. The output of to\_json looks  
something like this:

[{addressable:  
{attributes: {code: “123123123”, quantity: “123”, id: “1”,  
description: “Window”, unit\_price: “12”}},  
attributes: {addressable\_type: “WorkshopOrderPart”,  
workshop\_order\_id: “1”, addressable\_id: “1”, id: “1”, position:  
“1”}},  
{addressable: …  
}}]

You will notice that “attibutes” shows up twice due to the polymorphic  
relationship but is not a range so if you assign it to a javascript  
array then addressing it goes like this:

newarray[0].addressable.attributes.code

That works but:

newarray[0].addressable.attributes.addressable\_type

does not. Anyone know how to go about addressing the “attributes”?
