Rails’ to_xml w/ multiple associations.

This is a pain and took me about an hour to figure out today.

Rails ActiveRecord instances offer a nice function to render xml:

my_model.to_xml()

This method can be fed with a parameter

:include => []

to have all associations being integrated in the XML tree, just like when calling

my_model = MyModel.find( :include => [association])

But when I tried to use it, it always failed when dealing with nested associations. Until I found out the trick.
This is how it works: Basically you need to build a list of nested hashes.

class Book < ActiveRecord::Base
  has_many :pages
  has_one :owner
end
class Owner < ActiveRecord::Base
  has_many :books
end
class Page < ActiveRecord::Base
  has_many :spots_of_coffees
  belongs_to :book
end
class SpotOfCoffee < ActiveRecord::Base
  belongs_to :page
end

Now, you want to create a nice XML output containing a book, with its owner, pages and all spots of coffee?
Pretty easy:

  @book = Book.first
  includes = {} # let's build a hash; it's easier to read. At least for me...
  includes[:owner] = {} # owner has no association. So, let's take an empty hash as target
  includes[:pages] = { :include => :spots_of_coffee } #load pages and include its spots of coffee
    
  respond_to do |format|
    format.xml  { render :text => @book.to_xml(:include => includes) }
  end

One Reply to “Rails’ to_xml w/ multiple associations.”

  1. greate!!! exactly what i was looking for… it’s a little bit stupid that the to_xml-include and the AR-find-include have not the same schema… well anyway… thank you!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.