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?

On Jan 26, 11:42 am, Stephan H. [email protected] wrote:

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 C. wrote:

On Jan 26, 11:42�am, Stephan H. [email protected] wrote:

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

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

On Jan 26, 12:02 pm, Stephan H. [email protected] 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 C. wrote:

On Jan 26, 12:02�pm, Stephan H. [email protected] 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?

On Jan 26, 12:40 pm, Stephan H. [email protected] 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

Frederick C. wrote:

On Jan 26, 12:40�pm, Stephan H. [email protected] 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.

On Jan 26, 12:48 pm, Stephan H. [email protected] 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

Frederick C. wrote:

On Jan 26, 12:48�pm, Stephan H. [email protected] 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.

On Jan 26, 1:19 pm, Stephan H. [email protected] wrote:

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 C. wrote:

On Jan 26, 1:19�pm, Stephan H. [email protected] wrote:

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

now i get it, thank u so much!