Limit attributes written out by to_yaml

I want to write out a series of records to a text file so that I can
quickly edit them and then upload then again into the database. To do
this I am using YAML and some code that looks like this:

item = Item.find 153 ‘item.yaml’, ‘w’ ) do |out|
YAML.dump( item, out )

This dumps out all the field names and values which are stored in the
attributes hash. The output looks something like this:

  • !ruby/object:Item
    image_height: ‘’
    maximum_value: ‘’
    minimum_value: ‘’
    item_id: 153
    default_value: ‘’
    … many more fields

However I don’t want all these fields. How can I limit the YAML output
to just include the fields that I want?

I know that I can override to_yaml_properties to specify the fields that
i want from a class. But that really only lets me choose whether I want
to export @attributes or not. I think that I could override
to_yaml_properties of @attributes. Something like this

class Item
class <<@attributes
def to_yaml_properties
# …?

But I’m not too sure what I should write there?
Or maybe I should be overriding def [email protected]_yaml ?

Does anyone have any ideas?


  image_alignment: ''
  display_format: Vertical
  item_sub_text: ''

  page_number: 5
  email_address_yn: N
  survey_id: 153
  other_yn: N
  random_answer_order_yn: N
  database_dsn: ''
  image_width: ''
  item_text: "<span style=\"font-weight:bold\">When a good has 

positive network externalities,
this means that when using the product:"
order_number: 26
item_alias: ‘’
image_path: ‘’
item_type_id: 3
database_sql: ‘’
other_text: “Other, please specify”
required_yn: N

However there

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