to_xml with :only and :methods

Posted by Jake on Stack Overflow See other posts from Stack Overflow or by Jake
Published on 2011-02-22T21:37:33Z Indexed on 2011/02/22 23:25 UTC
Read the original article Hit count: 341

Filed under:
|

I'm calling to_xml on an ActiveRecord object with both :only and :methods parameters.

The method that I'm including returns a collection for AR objects. This works fine without the :only param, but when that is added I just get the default to_s representation of my objects.

i.e

<author><books>#&lt;Book:0x107753228&gt;</books>\n</author>

Any ideas?

Update, here is the code:

class Author < ActiveRecord::Base
  def books
    #this is a named scope
    products.by_type(:book)
  end
end

Author.to_xml(:methods => :books, :only => :id)

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby