Dumping selected attributes to YAML

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

On Mar 21, 4:05 am, Andrew B. [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs