Solved

Creating nested hash from nested active record results

Posted on 2013-05-22
4
126 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
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…
The viewer will learn how to count occurrences of each item in an array.
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.

705 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