Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating nested hash from nested active record results

Status
Not open for further replies.

c73mr0ck

Programmer
Oct 25, 2012
11
0
0
US
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]}]

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]}]

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top