简体   繁体   English

如何在Rails中加载此yml文件?

[英]How do I load this yml file in rails?

I received this yml data file from a colleague who is unreachable. 我从无法联系到的同事那里收到了这个yml数据文件。 I need to load it to get the data, but I'm not sure how: 我需要加载它才能获取数据,但不确定如何:

---
- !ruby/object:Question
  concise_attributes:
  - !ruby/object:ActiveModel::Attribute::FromDatabase
    name: id
    value_before_type_cast: 1
  - !ruby/object:ActiveModel::Attribute::FromDatabase
    name: name
    value_before_type_cast: Trip Summary
  - !ruby/object:ActiveModel::Attribute::FromDatabase
    name: question
    value_before_type_cast: Please state your general feelings about itinerary, the
      group, and the operations. How did it go overall?
  - !ruby/object:ActiveModel::Attribute::FromDatabase
    name: created_at
  - !ruby/object:ActiveModel::Attribute::FromDatabase
    name: updated_at
  new_record: false
  active_record_yaml_version: 2
- !ruby/object:Question
  concise_attributes:
  - !ruby/object:ActiveModel::Attribute::FromDatabase
    name: id
    value_before_type_cast: 2
  - !ruby/object:ActiveModel::Attribute::FromDatabase
    name: name
    value_before_type_cast: Itinerary
  - !ruby/object:ActiveModel::Attribute::FromDatabase
    name: question
    value_before_type_cast: Did you and the clients like the itinerary? If not, why
      not? How could it be improved? Was it accurate? Did you make any adjustments
      to the itinerary? Please describe any special things you did on the trip that
      were not part of the itinerary.
  - !ruby/object:ActiveModel::Attribute::FromDatabase
    name: created_at
  - !ruby/object:ActiveModel::Attribute::FromDatabase
    name: updated_at
  new_record: false
  active_record_yaml_version: 2

My first guess was to use the yaml_db gem, and then YAML::load_file itself directly, but I get the error: 我的第一个猜测是使用yaml_db gem,然后直接使用YAML :: load_file本身,但出现错误:

ArgumentError: undefined class/module Question

Even though there is a Question model in the app" 即使应用程序中有一个问题模型”

class Question < ActiveRecord::Base
end

It appears to have been encoded with ActiveModel::AttributeSet::YAMLEncoder . 它似乎已使用ActiveModel :: AttributeSet :: YAMLEncoder编码。 You can use the decode method to parse the yaml file. 您可以使用decode方法来解析yaml文件。 The use of the concise_attributes is idiosyncratic to YAMLEncoder. concise_attributes的使用与concise_attributes是特有的。

Rails Source Code Rails源代码

EDIT: While the file is under ActiveModel, to load it in a console use ActiveRecord::AttributeSet::YAMLEncoder 编辑:当文件在ActiveModel下时,要在控制台中加载它,请使用ActiveRecord::AttributeSet::YAMLEncoder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM