Custom to_xml

I have two models: ledger and label. A ledger can contain many
labels.

Here is the xml returned from http://localhost/ledgers/78.xml:

11 30.0 2007-10-22 2007-10-22 Deposit 78

Question:

How can I return the labels associated with the ledger? The xml should
look like this (notice the “labels” addition):

11 30.0 2007-10-22 2007-10-22 Deposit 78 Food Music

I just figured it out.

Instead of this: format.xml { render :xml => @ledger.to_xml }

do this: format.xml { render :xml => @ledger.to_xml(:include =>
[ :labels ]) }

Works like a charm. :slight_smile:

FYI I found the answer here:
http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html#M000910

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs