Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 382
Andrei Botalov Avatar asked Aug 23 '13 18:08

Andrei Botalov


2 Answers

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
like image 111
mchail Avatar answered Sep 30 '22 02:09

mchail


to_yaml ignores the include-parameter ... but you could do the following:

Hash.from_xml(menu.to_xml include: :dishes).to_yaml
like image 21
aphilippi Avatar answered Sep 30 '22 01:09

aphilippi