Forum: Ruby on Rails Limit attributes written out by to_yaml

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5726e6410f03a6d52d1127fce76d02ad?d=identicon&s=25 Williak FISK (Guest)
on 2006-01-11 17:47
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
File.open( 'item.yaml', 'w' ) do |out|
  YAML.dump( item, out )
end

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
    attributes:
      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
      # ....?
    end
  end
end

But I'm not too sure what I should write there?
Or maybe I should be overriding def Item.@attributes.to_yaml ?

Does anyone have any ideas?

Thanks


      image_alignment: ''
      display_format: Vertical
      minimum_number_responses:
      item_sub_text: ''



      page_number: 5
      row_text_width:
      email_address_yn: N
      survey_id: 153
      other_yn: N
      random_answer_order_yn: N
      database_dsn: ''
      image_width: ''
      other_display_type:
      subitem_count:
      answer_total:
      item_text: "<span style=\"font-weight:bold\">When a good has
positive network externalities,
        this means that when using the product:</span>"
      order_number: 26
      item_alias: ''
      image_path: ''
      maximum_number_responses:
      maximum_length:
      item_type_id: 3
      database_sql: ''
      other_text: "Other, please specify"
      required_yn: N

However there
This topic is locked and can not be replied to.