# To\_xml

**URL:** <https://www.ruby-forum.com/t/to-xml/172971>\
**Category:** Rails\
**Created:** [July 30, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/to-xml/172971 "2009-07-30T17:28:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wouter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e99b99/32.png) [@wouter](https://www.ruby-forum.com/u/wouter)\
**Post date:** [July 30, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/to-xml/172971/1 "2009-07-30T17:28:03Z")

</div>

Hey all,

I have some problems creating a rendered xml file

this is my source

def show\_friends  
@profile = Profile.find(user)  
@list = @profile.send(‘friends’).find(:all, :limit =\> 20, :order  
=\> ‘RAND()’)  
@checkin = Checkin.find(:last, :conditions =\> {:profile\_id =\>  
@list})  
@place = @checkin.place  
respond\_to do |format|  
format.html {render}  
format.xml { render :xml =\> @list.to\_xml(:dasherize =\>  
false)}  
end  
end

And this give as output

wouter \* ...

What i want to do now is include the @place object in my xml

so like this:

wouter \* ... .. 

I know i can do :include =\> [:place] but this include all the places  
from that person. I only want to get the last place where the user  
checked in ( @checkin = Checkin.find(:last, :conditions =\>  
{:profile\_id =\> @list})  
@place = @checkin.place) . So i really have to include this, but  
I have tried many things but nothing works.

How can i do this?

Thank you,

Wouter

---

<div class="post-metadata">

**Author:** ![wouter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e99b99/32.png) [@wouter](https://www.ruby-forum.com/u/wouter)\
**Post date:** [July 30, 2009, 11:19pm UTC](https://www.ruby-forum.com/t/to-xml/172971/2 "2009-07-30T23:19:20Z")

</div>

I think creating a builder file in your views directory will be the  
easiest way to get exactly the xml output you want.

---

<div class="post-metadata">

**Author:** ![wouter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e99b99/32.png) [@wouter](https://www.ruby-forum.com/u/wouter)\
**Post date:** [July 31, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/to-xml/172971/3 "2009-07-31T21:07:30Z")

</div>

Hey Josh,

Thank you for your answer. I have implemented this in my model and  
controller and it works half.  
I will try to explain what the problem is.

This is my code from profile.rb

has\_many :place  
has\_many :checkin  
has\_one :last\_checkin, :class\_name =\>“Checkin”, :order =\>  
“created\_at desc”  
has\_one :last\_place, :through =\> :last\_checkin, :source =\> :place

so i get the last\_checkin from my database sorted by latest,  
last\_place is then the place source from my last\_checkin.  
when i get the to\_xml and have as include :last\_checkin i see the  
latest checkin

\<last\_checkin\>  
\<created\_at type=“datetime”\>2009-07-31T19:00:49Z\</created\_at\>  
  
100  
\<places\_id type=“integer”\>59\</places\_id\>  
\<profile\_id type=“integer”\>3\</profile\_id\>  
  
\</last\_checkin\>

my places\_id is here 59 so i want to get the source of this place. But  
now when i do :last\_place i get this

\<last\_place\>

Namur BE 2009-07-31T18:43:50Z 57 50.4641 4.86043 Namen 2009-07-31T18:59:58Z

The id of place here is 57.

How does this comes and what am i doing wrong?

Thank you for your help!

Wouter

---

<div class="post-metadata">

**Author:** ![wouter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e99b99/32.png) [@wouter](https://www.ruby-forum.com/u/wouter)\
**Post date:** [August 1, 2009, 1:21am UTC](https://www.ruby-forum.com/t/to-xml/172971/4 "2009-08-01T01:21:32Z")

</div>

I took a look at my own code where I test implemented this and  
realized I have the same problem.

After watching SQL in the log and playing around in script/console I  
realized this was happening

If I type user.last\_checkin.last\_place I was getting the correct place  
if I typed user.last\_place it wasn’t sorting by created\_at anymore in  
the query.  
The same thing is happening in the to\_xml…

This feels a little dirty to me, but it seems to work fine. Just  
change the line in your model describing last\_place to include the  
ordered at on checkin table.

has\_one :last\_place, :through =\> :last\_checkin, :source  
=\> :place, :order =\> ‘checkin.created\_at desc’

and you should see the correct ‘last\_place’

Let me know if this worked for you.

---

<div class="post-metadata">

**Author:** ![wouter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e99b99/32.png) [@wouter](https://www.ruby-forum.com/u/wouter)\
**Post date:** [August 13, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/to-xml/172971/5 "2009-08-13T18:59:55Z")

</div>

Hey,

Thank you so much!

It worked

SOrry for late answer but forgot to answer!

THANK YOU!

---

<div class="post-metadata">

**Author:** ![wouter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e99b99/32.png) [@wouter](https://www.ruby-forum.com/u/wouter)\
**Post date:** [July 31, 2009, 7:15am UTC](https://www.ruby-forum.com/t/to-xml/172971/6 "2009-07-31T07:15:08Z")

</div>

I think the most efficient way would be to create a has\_one in the  
model with a unique name and use the conditions or order to get just  
one entry. I just tested this on one of my apps and it worked pretty  
well.  
I assume here that friends are just other profiles and a profile has  
many checkins?  
so you probably have something similar in profile.rb  
has\_many :checkins  
has\_many :places, :through =\> :checkins  
Add (I think anyways)  
has\_one :last\_checkin, :class\_name =\> “CheckIn”, :order =\>  
‘created\_at’  
has\_one :last\_place, :through =\> :last\_checkin, :source =\> :place

And you can just do

@profile.send(‘friends’).find(:all, :limit =\> 20, :order =\> ‘RAND()’,  
include =\> :last\_place) .to\_xml(:include =\> :last\_place)

- I put :last place in the finder to eager load, and again in the  
to\_xml so that is is included in the xml.

Another way which doesn’t require the model modified or a builder and  
is pretty ineffiecent for sql queries is

@items = @profile.send(‘friends’).collect do |f|  
h = f.attributes  
h[‘place’] = f.checkins.last.place.attributes  
h  
end  
@items.to\_xml

Let me know if you still have problems and include the models.  
-Josh
