Solved

Creating nested hash from nested active record results

Posted on 2013-05-22
4
117 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now