gpt4 book ai didi

ruby-on-rails - 如何在 rails 中加载这个 yml 文件?

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:43 31 4
gpt4 key购买 nike

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

---
- !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 本身,但我得到了错误:

ArgumentError: undefined class/module Question

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

class Question < ActiveRecord::Base
end

最佳答案

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

Rails Source Code

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

关于ruby-on-rails - 如何在 rails 中加载这个 yml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54012348/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com