# .to\_xml on an index, custom set response by Object

**URL:** <https://www.ruby-forum.com/t/to-xml-on-an-index-custom-set-response-by-object/161418>\
**Category:** Rails\
**Created:** [February 26, 2009, 10:44am UTC](https://www.ruby-forum.com/t/to-xml-on-an-index-custom-set-response-by-object/161418 "2009-02-26T10:44:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattfs218](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mattfs218](https://www.ruby-forum.com/u/mattfs218)\
**Post date:** [February 26, 2009, 10:44am UTC](https://www.ruby-forum.com/t/to-xml-on-an-index-custom-set-response-by-object/161418/1 "2009-02-26T10:44:20Z")

</div>

I have a Contacts controller, that I’m using to output contacts when  
an index is called, but the XML that I will output for each contact  
varies. I’m not having any success using :proc or :method

:method could work, but the string my method returns, escapes the \<\>  
characters I’m using trying to “fake” XML mark-up.

Basically, each contact has many Kvs, and I only want to :include  
output some KV (not all) for each contact. I can :include kvs but that  
will include ALL. I cannot used proc, because there’s no way for proc  
to “know” which individual contact it’s being run for.

Can anyone recommend an easy way around this?

–matt

---

<div class="post-metadata">

**Author:** ![mattfs218](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mattfs218](https://www.ruby-forum.com/u/mattfs218)\
**Post date:** [February 26, 2009, 10:46am UTC](https://www.ruby-forum.com/t/to-xml-on-an-index-custom-set-response-by-object/161418/2 "2009-02-26T10:46:42Z")

</div>

On Feb 26, 9:28 am, MattFS218 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> to “know” which individual contact it’s being run for.  
> It seems like the easiest way would be a method that calls to\_xml on  
> those kvs you want included. Not sure what you meant about fake xml  
> mark-up in your comment above. Another way might be to :include an  
> association whose conditions restrict it to those Kvs you are  
> interested in.

Fred

Fred

---

<div class="post-metadata">

**Author:** ![mattfs218](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mattfs218](https://www.ruby-forum.com/u/mattfs218)\
**Post date:** [February 26, 2009, 11:09am UTC](https://www.ruby-forum.com/t/to-xml-on-an-index-custom-set-response-by-object/161418/3 "2009-02-26T11:09:40Z")

</div>

Hey Fred,

Thanks for the rapid response.

But when I

format.fxml { render :fxml =\> @contacts.to\_fxml(:methods=\>  
[:my\_action] ) }

and

def my\_action()  
return self.kvs.to\_xml  
end

the resulting XML has the \< and \> symbols escaped out \<?xml  
version="1.0"

Unless I’m mistaken, but associations are per Object, correct? Not per  
instance. the Kvs i’m trying to output vary per instance. Any other  
recommendations?

–matt

On Feb 26, 4:45 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mattfs218](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mattfs218](https://www.ruby-forum.com/u/mattfs218)\
**Post date:** [February 26, 2009, 11:17am UTC](https://www.ruby-forum.com/t/to-xml-on-an-index-custom-set-response-by-object/161418/4 "2009-02-26T11:17:18Z")

</div>

On Feb 26, 10:08 am, MattFS218 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def my\_action()  
> return self.kvs.to\_xml  
> end
> 
> the resulting XML has the \< and \> symbols escaped out \<?xml  
> version="1.0"

Oh I get you.

> Unless I’m mistaken, but associations are per Object, correct? Not per  
> instance. the Kvs i’m trying to output vary per instance. Any other  
> recommendations?  
> You can have interpolated conditions on an association (but this does  
> rely on you being able to express your conditions in sql, prevents  
> eager loading etc…). Another way is a custom to\_xml method on your  
> model, you’ll get a Builder::XmlMarkup object given to you on which  
> you can generate any xml you want

Fred

---

<div class="post-metadata">

**Author:** ![mattfs218](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mattfs218](https://www.ruby-forum.com/u/mattfs218)\
**Post date:** [February 26, 2009, 11:39am UTC](https://www.ruby-forum.com/t/to-xml-on-an-index-custom-set-response-by-object/161418/5 "2009-02-26T11:39:04Z")

</div>

is it possible to get a quick example of how to override .to\_xml?, and  
maybe add a little bit of markup?

On Feb 26, 5:16 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mattfs218](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mattfs218](https://www.ruby-forum.com/u/mattfs218)\
**Post date:** [February 26, 2009, 12:01pm UTC](https://www.ruby-forum.com/t/to-xml-on-an-index-custom-set-response-by-object/161418/6 "2009-02-26T12:01:23Z")

</div>

On 26 Feb 2009, at 10:37, MattFS218 wrote:

> is it possible to get a quick example of how to override .to\_xml?, and  
> maybe add a little bit of markup?

THere’s an example in the documentation for to\_xml.

Fred

---

<div class="post-metadata">

**Author:** ![mattfs218](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mattfs218](https://www.ruby-forum.com/u/mattfs218)\
**Post date:** [February 26, 2009, 12:06pm UTC](https://www.ruby-forum.com/t/to-xml-on-an-index-custom-set-response-by-object/161418/7 "2009-02-26T12:06:41Z")

</div>

The doc’s give an example of entirely overwriting the output. All I  
really wanna do is append another tag. I’ve tried using various  
combinations of super, but super is returning a string. Is it possible  
then, at least to get an example of how to override xml using super,  
and appending a tag? I’ve been working on this for the past 3 hrs. Any  
help would be greatly appreciated.

–matt

On Feb 26, 6:00 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
