# Restrict attributes in to\_xml

**URL:** <https://www.ruby-forum.com/t/restrict-attributes-in-to-xml/85716>\
**Category:** Rails\
**Created:** [February 15, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/restrict-attributes-in-to-xml/85716 "2007-02-15T20:01:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [February 15, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/restrict-attributes-in-to-xml/85716/1 "2007-02-15T20:01:49Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [February 15, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/restrict-attributes-in-to-xml/85716/2 "2007-02-15T20:08:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [February 15, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/restrict-attributes-in-to-xml/85716/3 "2007-02-15T20:10:59Z")

</div>

[http://www.railsmanual.org/module/ActiveRecord%3A%3AXmlSerialization/to\_xml](http://www.railsmanual.org/module/ActiveRecord%3A%3AXmlSerialization/to_xml)

@user.to\_xml(:except =\> :pw\_hash, :pw\_salt)

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [February 15, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/restrict-attributes-in-to-xml/85716/4 "2007-02-15T20:26:01Z")

</div>

On 2/15/07, Ben J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)
