to_xml method

Hey, i have overwritten the to_xml method with these: http://pastie.org/793456 and i call it with: render :xml => Focat.find(:all) but the xml elements are displayed twice! could someone see my failure?

Hey, i have overwritten the to_xml method with these:http://pastie.org/793456 and i call it with: render :xml => Focat.find(:all) but the xml elements are displayed twice! could someone see my failure?

rails will call to_xml on all each member of the collection, but it looks like your to_xml is doing a second Focat.find(:all).

Fred

Frederick Cheung wrote:

i found out that to_xml is called twice. but how can i solve this problem?

Like i said you shouldn't be doing that Focat.all inside your to_xml method - rails is already iterating over that collection for you

Fred

Frederick Cheung wrote:

Frederick Cheung wrote:

>> i found out that to_xml is called twice. but how can i solve this >> problem?

> Like i said you shouldn't be doing that Focat.all inside your to_xml > method - rails is already iterating over that collection for you

> Fred

how does rails that do?

You've called render :xml, passing an array. Rails calls to_xml on the array, which in turns calls to_xml on every element of the array.

Fred

Frederick Cheung wrote:

Frederick Cheung wrote:

>> how does rails that do?

> You've called render :xml, passing an array. Rails calls to_xml on the > array, which in turns calls to_xml on every element of the array.

> Fred

ok, but i need this strukture of the xml file, where i show focats and fotos.

that's fine, but if you want to be able to write render :xml => Focat.all then you need to let rails do the outermost iteration. You may not care about that at all though - you can generate your xml separately and then do render :xml => some_xml Fred

Frederick Cheung wrote:

Frederick Cheung wrote: >> ok, but i need this strukture of the xml file, where i show focats and >> fotos.

> that's fine, but if you want to be able to write render :xml => > Focat.all then you need to let rails do the outermost iteration. You > may not care about that at all though - you can generate your xml > separately and then do render :xml => some_xml > Fred

hey, now i tried it with: @focats = Focat.find(:all) xml = @focats.to_xml render :xml => xml but also the entries are twice. i dont get it.

This code is basically identical to the previous version (it's just spelling out what rails was doing for you). The to_xml method on arrays calls to_xml on every single object in the array.

Fred

Frederick Cheung wrote: