{"id":94,"date":"2009-05-14T15:43:55","date_gmt":"2009-05-14T14:43:55","guid":{"rendered":"http:\/\/www.any-where.de\/blog\/?p=94"},"modified":"2009-05-14T15:45:22","modified_gmt":"2009-05-14T14:45:22","slug":"rails-to_xml-with-multiple-associations","status":"publish","type":"post","link":"https:\/\/www.any-where.de\/blog\/rails-to_xml-with-multiple-associations\/","title":{"rendered":"Rails&#8217; to_xml w\/ multiple associations."},"content":{"rendered":"<p>This is a pain and took me about an hour to figure out today.<\/p>\n<p>Rails ActiveRecord instances offer a nice function to render xml:<\/p>\n<pre>my_model.to_xml()<\/pre>\n<p>This method can be fed with a parameter <\/p>\n<pre>:include => []<\/pre>\n<p> to have all associations being integrated in the XML tree, just like when calling<\/p>\n<pre>my_model = MyModel.find( :include =&gt; [association])<\/pre>\n<p>But when I tried to use it, it always failed when dealing with nested associations. Until I found out the trick.<br \/>\nThis is how it works: Basically you need to build a list of nested hashes.<\/p>\n<pre>class Book &lt; ActiveRecord::Base\r\n  has_many :pages\r\n  has_one :owner\r\nend<\/pre>\n<pre>class Owner &lt; ActiveRecord::Base\r\n  has_many :books\r\nend<\/pre>\n<pre>class Page &lt; ActiveRecord::Base\r\n  has_many :spots_of_coffees\r\n  belongs_to :book\r\nend<\/pre>\n<pre>class SpotOfCoffee &lt; ActiveRecord::Base\r\n  belongs_to :page\r\nend<\/pre>\n<p>Now, you want to create a nice XML output containing a book, with its owner, pages and all spots of coffee?<br \/>\nPretty easy:<\/p>\n<pre>\r\n  @book = Book.first\r\n  includes = {} # let's build a hash; it's easier to read. At least for me...\r\n  includes[:owner] = {} # owner has no association. So, let's take an empty hash as target\r\n  includes[:pages] = { :include => :spots_of_coffee } #load pages and include its spots of coffee\r\n    \r\n  respond_to do |format|\r\n    format.xml  { render :text => @book.to_xml(:include => includes) }\r\n  end\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 =&gt; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.any-where.de\/blog\/rails-to_xml-with-multiple-associations\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Rails&#8217; to_xml w\/ multiple associations.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,25],"tags":[35,33,27,34],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-programming-the-web","category-ruby-on-rails","tag-multiple-associations","tag-rails","tag-ruby","tag-to_xml"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":7,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":100,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions\/100"}],"wp:attachment":[{"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.any-where.de\/blog\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}