I have an array of hashes like so, I have removed some of the records for presentation purposes.
[
{
"id"=>425343308605714432,
"text"=>"Hello",
"sender"=> {
"id"=>1375480994,
"name"=>"Rocket"
},
"sender_id"=>1375480994,
"recipient"=> {
"id"=>24766510,
"name"=>"Max Rose-Collins"
},
"recipient_id"=>24766510,
"recipient_id_str"=>"24766510",
"recipient_screen_name"=>"maxrosecollins",
"created_at"=>"Mon Jan 20 19:05:21 +0000 2014"
},
{
"id"=>413305410666639361,
"text"=>"How are you",
"sender"=> {
"id"=>24766510,
"name"=>"Max Rose-Collins"
},
"sender_id"=>24766510,
"sender_screen_name"=>"maxrosecollins",
"recipient"=> {
"id"=>1375480994,
"name"=>"Rocket"
},
"recipient_id"=>1375480994,
"recipient_id_str"=>"1375480994",
"recipient_screen_name"=>"Rocket",
"created_at"=>"Wed Dec 18 13:51:03 +0000 2013"
}
]
This array of hashes also contains other messages between other users. I want to arrange this into conversations which contain the sent and received messages between two users. I have tried using
.group_by { |r| [r['recipient_id'], r['sender_id']] }
but this doesn't group them how I would have thought. It makes two groups one sent and the other received.
{
[1375480994, 24766510]=>[removed for presentation],
[24766510, 1375480994]=>[removed for presentation],
[24766510, 146385359]=>[removed for presentation],
[146385359, 24766510]=>[removed for presentation],
}
How can I merge these two hashes into one so I have the whole of the conversation in one place, like a threaded conversation?
为了通过对话对它们进行分组,您可以将sort
方法添加到group_by
函数中:
.group_by{ |r| r.values_at('recipient_id', 'sender_id').sort }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.