# Creation of XML with objects

**URL:** <https://www.ruby-forum.com/t/creation-of-xml-with-objects/192974>\
**Category:** Rails\
**Created:** [August 9, 2010, 12:15pm UTC](https://www.ruby-forum.com/t/creation-of-xml-with-objects/192974 "2010-08-09T12:15:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 9, 2010, 12:15pm UTC](https://www.ruby-forum.com/t/creation-of-xml-with-objects/192974/1 "2010-08-09T12:15:13Z")

</div>

Hi,  
I am trying to get the response in xml format.  
For that I have written following code

def user\_details  
@user=User.find(params[:id])  
@company=@user.company  
respond\_to do |format|  
format.xml {render :xml=\> @user.to\_xml(:include =\> @company)}  
end  
end

I am getting following error:-  
“undefined method `macro’ for nil:NilClass”

Can anyone tell, what is wrong with the code?

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 9, 2010, 12:37pm UTC](https://www.ruby-forum.com/t/creation-of-xml-with-objects/192974/2 "2010-08-09T12:37:32Z")

</div>

On Aug 9, 11:15 am, Mike D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> I am trying to get the response in xml format.  
> For that I have written following code
> 
> def user\_details  
> @user=User.find(params[:id])  
> @removed\_email\_address@domain.invalid  
> respond\_to do |format|  
> format.xml {render :xml=\> @user.to\_xml(:include =\> @company)}

you should be giving the name of the association - not the actual  
associated object

Fred

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 9, 2010, 1:00pm UTC](https://www.ruby-forum.com/t/creation-of-xml-with-objects/192974/3 "2010-08-09T13:00:23Z")

</div>

Hi Fred,  
How can I create user defined xml structure?

My previous code gives me xml like:  
  
Mike\>  
removed\_email\_address@domain.invalid  
  
2  
ABC

I need to create a XML like this:-

 Mike removed\_email\_address@domain.invalid 2

Any help appreciated.

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 9, 2010, 2:52pm UTC](https://www.ruby-forum.com/t/creation-of-xml-with-objects/192974/4 "2010-08-09T14:52:47Z")

</div>

On Aug 9, 12:00 pm, Mike D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need to create a XML like this:-
> 
> Mike removed\_email\_address@domain.invalid 2

If the default to\_xml isn’t giving you want you want you can override  
it yourself (to\_xml does take quite a few options though - have a play  
with them)

Fred

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 9, 2010, 2:59pm UTC](https://www.ruby-forum.com/t/creation-of-xml-with-objects/192974/5 "2010-08-09T14:59:14Z")

</div>

Hello,

if you remove the :include =\> :company only the company\_id will be  
serialized.

The following code should work as you expect : render :xml =\> @user

Mickael

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 9, 2010, 3:16pm UTC](https://www.ruby-forum.com/t/creation-of-xml-with-objects/192974/6 "2010-08-09T15:16:08Z")

</div>

Hi,  
I need the “company\_id” in the XML.

render :xml=\> @user will only include the user attributes.

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [August 9, 2010, 3:25pm UTC](https://www.ruby-forum.com/t/creation-of-xml-with-objects/192974/7 "2010-08-09T15:25:00Z")

</div>

On Aug 9, 2:16 pm, Mike D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> I need the “company\_id” in the XML.
> 
> render :xml=\> @user will only include the user attributes.

Is the assocation user has\_one company ? If so (ie user has no  
company\_id attribute) then the easiest thing is probably to write a  
method on user that returns the appropriate id and then use  
the :methods option to to\_xml to include that in the output

Fred
