Render XML

Would be helpful to know more about the XML render syntax. In this case,

format.xml { render :xml => @product.to_xml(:include => :orders) }

Seems to suffice with no template….

Render JSON

format.json {render :json => @product.to_json(:include => :orders)}

Render HTML

format.html who_bought.html.erb

<% @product.orders.each do |order| %>
  <h2>Order <%= order.id %></h2>
  <p>Shipped to <%= order.address %></p>
  <table>
    <tr>
      <th>Product</th>
      <th>Quantity</th>
      <th>Total Price</th>
    </tr>
    <% order.line_items.each do |item| %>
      <tr>
        <td><%= item.product.title %></td>
        <td><%= item.quantity %></td>
        <td><%= number_to_currency item.total_price %></td>
      </tr>
    <% end %>
    <tr>
      <th colspan="2">total</th>
      <th><%= number_to_currency order.line_items.map(&:total_price).sum %></th>
    </tr>
  </table>
  <p>Paid by <%= order.pay_type %>
<% end %>

Research

The Rails API is your friend! to_xml() can be passed options that help you to fine-tune the output. format.xml { render :xml => @product.to_xml(:include => :orders) } works but does not get you output similar to the atom feed. To get XML similar to what was in the example atom feed by just using to_xml():

        format.xml { render( :xml => @product.to_xml(
           :only => [ :title, :updated_at ],
           :skip_types => true,
           :include => { 
              :orders => {
                 :except => [ :created_at, :updated_at ],
                 :skip_types => true,
                 :include => { 
                    :line_items => {
                       :skip_types => true,
                       :except => [ :created_at, :updated_at, :cart_id, :order_id ]
                    }
                 }
              }
           }
        )) }

There’s a link on that same page to using as_json() to generate similar output, using the same options (except :skip_types).