Solved

Creating nested hash from nested active record results

Posted on 2013-05-22
4
123 Views
Last Modified: 2014-10-27
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:
 
results_to_hash = Hash[ found_categories.map{ |c| [c.id, c.title]}]

Open in new window


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:

results_to_hash = Hash[ found_categories.map{ |c| [c.id, c.title, c.categories]}]

Open in new window


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
0
Comment
Question by:clemcrock
4 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 39265035
It might be that you need a deep copy of the collection. AFAIK are assignments to array just "shallow" that means if you have a collection [1,2,3]

and you add it to a hash
ht.Add(key => #someKey, value => #collection)
and you change a collection thereafter the  hash table collection gets influenced also.

So in this case you probably want to use clone

If not please provide code that we can use.

Regards
0
 

Accepted Solution

by:
clemcrock earned 0 total points
ID: 39757672
Just an FIY - this was my solution:

def self.active_record_to_array_of_hashes(array_collection, sub_collection_names=[])
     hashed_collection = []
     array_collection.each do |obj|   
       tmp_hash = ActiveSupport::JSON.decode(obj.to_json)
     
       #### hashify specified model association
       sub_collection_names.each do |name| 
         if(obj.class.reflections.keys.include?(name))
           tmp_hash[name] = ActiveSupport::JSON.decode(obj.send(name).to_json)
         end
       end
       hashed_collection << tmp_hash
     end
     return hashed_collection
   end

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question