Would this work?
MyModel.last.serializable_hash methods: MyModel.reflections.keys
Or is that too much?
Look at the options that you can pass into serializable_hash.
However, if you are doing this for json/etc. serialization in the
controller, check out ActiveModel::Serializers.
On Wednesday, May 22, 2013 10:53:14 AM UTC-4, Ruby-Forum.com User wrote:
>
> Hello,
> I'm trying to find a very abstract and "one size fits all" for
> converting nested active record results to nested hashes. It's easy,
> to do one level deep as such:
>
> [code]
> results_to_hash = Hash[ found_categories.map{ |c| [c.id, c.title]}]
> [/code]
>
> But, when I try to add another collection to the mix, it completely
> borks and the results_to_hash only returns an empty hash IE:
>
> [code]
> results_to_hash = Hash[ found_categories.map{ |c| [c.id, c.title,
> c.categories]}]
> [/code]
>
> Ultimately, I'd like it to be smart enough to detect if a model object
> contains a collection (IE: object.class.reflect_on_all_associations),
> and automatically convert those to hashes.
>
>
> Any ideas?
>
> Thanks,
> Eric
>
> --
> Posted via http://www.ruby-forum.com/.
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/22afeaa7-be1b-4837-9eae-05ef72c14303%40googlegroups.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.