Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 根据时间戳将数据分成多个散列_Ruby On Rails_Ruby_Arrays_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 根据时间戳将数据分成多个散列

Ruby on rails 根据时间戳将数据分成多个散列,ruby-on-rails,ruby,arrays,ruby-on-rails-3,Ruby On Rails,Ruby,Arrays,Ruby On Rails 3,我有一个通知页面。就像社交网站上的一般通知页面一样。每个通知都有一个unix时间戳。我想根据天数将通知分成多维散列。哈希键可以表示它的日期。例: notification['3July'][0] = 'Answered a question' notification['3July'][1] = 'Answer got 13 upvotes' notification['3July'][2] = 'Earned a badge' .... notification['23June'][0]

我有一个通知页面。就像社交网站上的一般通知页面一样。每个通知都有一个unix时间戳。我想根据天数将通知分成多维散列。哈希键可以表示它的日期。例:

notification['3July'][0] = 'Answered a question'
notification['3July'][1] = 'Answer got 13 upvotes'
notification['3July'][2] = 'Earned a badge'
....

notification['23June'][0]  = 'Set a bounty of 50 credits'
notification['23June'][1]  = 'Followed XYZ'
notification['23June'][2]  = 'Added a question on stack overflow'
然后,我将迭代多维散列,并在每天下发布通知。目前,我从数据库获取数据,其格式如下:

notification[0] = { 'text' => 'Answered a question' , 'timestamp' => 123456 }
notification[1] = { 'text' => 'Answer got 13 upvotes'  , 'timestamp' => 123456 }
notification[2] = { 'text' => 'Earned a badge'  , 'timestamp' => 123456 }
....

notification[3]  = { 'text' =>'Set a bounty of 50 credits'  , 'timestamp' => 123456 }
notification[4]  = { 'text' =>'Followed XYZ'  , 'timestamp' => 123456 }
notification[5]  = { 'text' =>'Added a question on stack overflow'  , 'timestamp' => 123456 }

在Ruby/Rails中,实现这一点的最干净和最佳方法是什么?

按时间戳排序通知,添加一列以选择只包含日期的内容,然后在Ruby中使用
groupby
。这应该是最简单的方法

要选择要使用的其他列,您应该为其添加
attr\u访问器


Array#[]
无法将字符串作为参数。已编辑问题。已将数组更改为哈希。类似于此,您将显示为解决此问题而编写的代码。有关详细信息,请参阅。我们将立即尝试此操作。如果可行,请接受。稍微更新了描述。