generating XML in rails

Posted by PeterWong on Stack Overflow See other posts from Stack Overflow or by PeterWong
Published on 2010-06-09T07:20:03Z Indexed on 2010/06/09 7:32 UTC
Read the original article Hit count: 228

Filed under:
|

I created a controller having an action:

def gen_books_xml
  @books = Book.find(:all, :conditions => {:owner_id => 1})
  respond_to do |format|
    format.xml { render :xml => @books.to_xml(:root => "Books", :skip_types=>true) }
  end
end

How could I implement the to_xml method in the Book model sa that it can generate the following format?

<?xml version="1.0" encoding="UTF-8"?>
<Books>
  <Owner>1</Owner>
  <Book><title>some title</title></Book>
  <Book><title>some title</title></Book>
  <Book><title>some title</title></Book>
  ...
</Books>

where there is only 1 Owner element and many Book elements

I can only output the Book elements but cannot output the one Owner in the same level of Books. Please HELP!!!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about Xml