Nic*_*nto 38 ruby group-by ruby-on-rails-3
我有一个@bunch作为数组返回的模型
每个模型都有属性 - commentable_id和commentable_type(多态关联)
我想通过可评论的方式对模型进行分组,但如果我这样做的话
@bunch.group_by(&:commentable)
Run Code Online (Sandbox Code Playgroud)
它还从数据库中获取可评论,这是不需要的.
我可以做@bunch.group_by(&:commentable_id)但这会引起一些混淆,因为可以有几种类型的可评论模型
有没有办法去group_by commentable_idAND commentable_type?
saw*_*awa 83
为什么不这样做:
@bunch.group_by{|e| [e.commentable_id, e.commentable_type]}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13764 次 |
| 最近记录: |