-3

我有一个通知页面。就像社交网站上的任何通用通知页面一样。每个通知都有一个 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 中最干净和最优化的方法是什么?

4

1 回答 1

1

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

要选择要工作的其他列,您应该attr_accessor为其添加。

http://www.ruby-doc.org/core-2.0/Enumerable.html#method-i-group_by

于 2013-07-03T08:05:41.420 回答