7

我想以 YAML 格式打印 ActiveRecord 模型以进行调试。

目前我调用model.to_yaml. 但它不返回模型的关联

如何将具有关联之一的模型转换为 YAML 格式?

4

2 回答 2

11

You can convert to json first. The default ActiveRecord as_json method allows you to include assocations. From there, it's straightforward to convert to yaml. Example:

menu.as_json(include: :dishes).to_yaml
于 2013-08-23T18:51:35.997 回答
4

to_yaml 忽略包含参数...但您可以执行以下操作:

Hash.from_xml(menu.to_xml include: :dishes).to_yaml
于 2013-12-11T11:10:36.650 回答