首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在rails中加载这个yml文件?

如何在rails中加载这个yml文件?
EN

Stack Overflow用户
提问于 2019-01-03 03:53:53
回答 1查看 209关注 0票数 2

我从一个联系不上的同事那里收到了这个yml数据文件。我需要加载它来获取数据,但我不确定如何加载:

代码语言:javascript
运行
复制
---
- !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

我的第一个猜测是使用yaml_db gem,然后直接使用YAML::load_file本身,但我得到了错误:

代码语言:javascript
运行
复制
ArgumentError: undefined class/module Question

即使应用程序中有一个问题模型“

代码语言:javascript
运行
复制
class Question < ActiveRecord::Base
end
EN

回答 1

Stack Overflow用户

发布于 2019-01-03 04:11:43

它似乎是用ActiveModel::AttributeSet::YAMLEncoder编码的。您可以使用decode方法来解析yaml文件。concise_attributes的使用是YAMLEncoder所特有的。

Rails Source Code

编辑:当文件在ActiveModel下时,使用ActiveRecord::AttributeSet::YAMLEncoder在控制台中加载它

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54012348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档