Restrict attributes in to_xml

For example, let’s say I have a User model with 2 attributes:

  1. pw_hash
  2. pw_salt

I don’t want these to show up in the to_xml method. How do I keep to_xml
from using these attributes?

Hey Ben

On Feb 15, 2007, at 9:01 AM, Ben J. wrote:

For example, let’s say I have a User model with 2 attributes:

  1. pw_hash
  2. pw_salt

I don’t want these to show up in the to_xml method. How do I keep
to_xml
from using these attributes?

You can specify what you don’t want using except:

user.to_xml :except => [: pw_hash, : pw_salt]

Andy

http://www.railsmanual.org/module/ActiveRecord%3A%3AXmlSerialization/to_xml

@user.to_xml(:except => :pw_hash, :pw_salt)

On 2/15/07, Ben J. [email protected] wrote:

For example, let’s say I have a User model with 2 attributes:

  1. pw_hash
  2. pw_salt

I don’t want these to show up in the to_xml method. How do I keep to_xml
from using these attributes?

Look at the :except option:
http://rails.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html#M000910

If you want to make this default you can just override it in your model:

to_xml(options = {})
super(options.update(:except => [:pw_hash, :pw_salt]))
end


Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com