I want to print ActiveRecord model in YAML format for debugging purposes.
Currently I invoke model.to_yaml
. But it doesn't return model's associations
How can I convert model with one of its associations to YAML format?
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
to_yaml ignores the include-parameter ... but you could do the following:
Hash.from_xml(menu.to_xml include: :dishes).to_yaml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With