Forum: Ruby on Rails to_json and polymorphic models

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4162ba12ff21a41b7b1d136f2491fe54?d=identicon&s=25 Jif (Guest)
on 2007-07-06 22:32
(Received via mailing list)
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"?
This topic is locked and can not be replied to.