Solved

Creating nested hash from nested active record results

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linux curl command to login form with random token including in POST data 10 104
exchange, scripts 3 69
Need to Modify a Script I found 5 99
Recursively Delete Files 5 83
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

823 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