我可以转换模型及其协会YAML格式之一? [英] Can I convert model with one of its associations to YAML format?

查看:105
本文介绍了我可以转换模型及其协会YAML格式之一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打印的ActiveRecord模型YAML格式进行调试。

I want to print ActiveRecord model in YAML format for debugging purposes.

目前我调用 model.to_yaml 。但是它不返回模型协会

Currently I invoke model.to_yaml. But it doesn't return model's associations

我怎么能转换模型及其协会YAML格式?

How can I convert model with one of its associations to YAML format?

推荐答案

您可以先转换成JSON。默认的ActiveRecord as_json 方法可以包括assocations。从那里,这是简单的转换为YAML。例如:

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

这篇关于我可以转换模型及其协会YAML格式之一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆