# Dumping selected attributes to YAML

**URL:** <https://www.ruby-forum.com/t/dumping-selected-attributes-to-yaml/132463>\
**Category:** Rails\
**Created:** [March 20, 2008, 12:52pm UTC](https://www.ruby-forum.com/t/dumping-selected-attributes-to-yaml/132463 "2008-03-20T12:52:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@Allan](https://www.ruby-forum.com/u/Allan)\
**Post date:** [March 20, 2008, 12:52pm UTC](https://www.ruby-forum.com/t/dumping-selected-attributes-to-yaml/132463/1 "2008-03-20T12:52:42Z")

</div>

Hi,

I am trying to dump an ActveRecord tree structure (created using  
acts\_as\_tree) with elements that also have associations.

I want to dump the YAML representation as text, edit it and then  
reload it. The current state of my project means that this is the  
most efficient way to get bulk changes working.

After spendign a day Googling I cannot see a way to dump a subset of  
the ActiveRecord attributes.

For example:

- !ruby/object:Expectation  
attributes:  
status: WRITTEN  
name: tpe2  
updated\_at: 2008-03-19 14:34:28  
evidence:  
metric\_table:  
order\_column\_name:  
goal\_diff\_filename:  
metric\_select\_column\_name:  
goal\_diff\_options:  
id: “2”  
order\_direction:  
goal\_numeric\_comparison:  
parent\_section\_id: “1”  
goal\_numeric\_value:  
owner: “222”  
documentation:  
reviewer:  
created\_at: 2008-03-19 14:34:28  
implementor:  
attributes\_cache: {}

is what I get when I dump an instance to YAML.

But I don’t want the \*id, created\_at and updated\_at fields, since  
they’re irrelevant to my needs and will change when I reload this  
data.

I can see how to\_yaml\_properties works for simple objects and indeed  
if I use that method to limit the instance variables I want to see  
then I get:

def to\_yaml\_properties  
props = self.attributes  
props.delete “id”  
props.delete “created\_at”  
props.delete “updated\_at”  
props.delete “parent\_id”  
props.delete “name”  
props.keys.map{|prop| “@#{prop}”}  
end

- !ruby/object:Expectation  
status:  
metric\_select\_action\_math:  
metric\_select\_action\_index:  
evidence:  
metric\_table:  
order\_column\_name:  
goal\_diff\_filename:  
metric\_select\_column\_name:  
goal\_diff\_options:  
testplan\_section\_id:  
order\_direction:  
goal\_numeric\_comparison:  
owner:  
goal\_numeric\_value:  
documentation:  
reviewer:  
implementor:  
goal\_regexp:

But now this cannot be reloaded as an Expectation object because these  
instance variables are now no longer in the attributes section as they  
were in the previous dump

Other than running a post-dump textual deletion of these entries I am  
completely stumped.

I note that the to\_xml methods allow this kind of feature but to\_yaml  
doesn’t seem to offer anything like this.

Allan

---

<div class="post-metadata">

**Author:** ![Allan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@Allan](https://www.ruby-forum.com/u/Allan)\
**Post date:** [March 27, 2008, 9:04am UTC](https://www.ruby-forum.com/t/dumping-selected-attributes-to-yaml/132463/2 "2008-03-27T09:04:36Z")

</div>

On Mar 21, 4:05 am, Andrew B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here is a bit of code I copied from techno-weenie and made some  
> modifications to.

I will take a thorough look at this and see where it gets me.

Thanks,

Allan
