How to dynamically add a hash to another hash in Ruby on Rails?


I’m extremely new to Ruby and can’t figure how to do this. Basically what I want is to convert this PHP code into Ruby code:

$some_array = ['user_id' => 23, 'parent_id' => 24, ...];
$another_array[$some_array['parent_id']]['respuestas'][] = $some_array;

I’ve been trying to figure out how to do it and have read several questions on this topic, but can’t find a proper solution. The above code will be inside a loop, so the $another_array[$some_array['parent_id']]['respuestas'] will have multiple items inside, I don’t know how many exactly because results come from a database.

Please I would appreciate any pointers into the right direction, I’m stuck in this 🙁

edit: this is the actual PHP code I’m trying to “translate” into Ruby

$comments = array();
foreach($ar as $item)
   $parent_array = get_some_array_back();
   $comments[$parent_array['parent_id']]['replies'][] = $item;

return $comments;

Edit 2: Here is the data from both arrays:

 $item = {
   ["comment_id"=> 1, "comment_text" => "text", "parent_id" => null ],
   ["comment_id"=> 2, "comment_text" => "text2", "parent_id" => 1],
 $parent_array = ["comment_id"=> 2, "commen_text" => "text", "parent_id"=>1]

So what I’m doing is I store in the $comments array all the comments that don’t have a parent_id, and then for the rest I do an array search to see if the parent_id is key in my $comments array. If it is then I add it as a new index “replies” an array of all the comments that have that as parent.


Would you please explain why the downvotes? I made sure this wasn’t a duplicate because I search a lot for it. If it is in fact duplicate, please kindly point to the question with the answer.
– Ant100
16 hours ago
This seems to be a pure-Ruby question, so I see no reason for the Rails’ tag.
I’m working on Rails so I didn’t know if it made a difference
– Ant100
12 hours ago



First get the parents and then get children using .map:

comments = [{comment_id: 1, parent_id: nil, text: "parent 1 "}, {comment_id: 2, parent_id: nil, text: "parent 2 "}, {comment_id: 3, parent_id: 1, text: "reply for comment 1"}, {comment_id: 4, parent_id: 2, text: "reply for comment 2"}] 

# get the parents first
parents ={ |comment| comment if comment[:parent_id] == nil }.compact

# children refers to each comment as parent with replies as children  

children = {|parent| parent.merge({children: {|children| children if children[:parent_id] == parent[:comment_id]}.compact })}


[{:comment_id=>1, :parent_id=>nil, :text=>"parent 1 ", :children=>[{:comment_id=>3, :parent_id=>1, :text=>"reply for comment 1"}]}, {:comment_id=>2, :parent_id=>nil, :text=>"parent 2 ", :children=>[{:comment_id=>4, :parent_id=>2, :text=>"reply for comment 2"}]}] 


That works for one dimesion, but I’m working with a multidimensional hash. I’m also trying to access a hash key using a variable I get from another hash, and then inside that hash create a new hash.
– Ant100
19 hours ago
Also that replaces value for ‘b’, what I would need is for b to become a new hash with different values 🙁
– Ant100
19 hours ago
You are trying to group the comments per user ?
@Ant100 can you show the data structure for both arrays.
@Ant100 I have updated my answer based on your data structure you provided