UNIQ在Ruby对象属性 [英] Uniq by object attribute in Ruby

查看:335
本文介绍了UNIQ在Ruby对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是选择了在一个阵列特有相对于一个或多个属性的对象最优雅的方式?

What's the most elegant way to select out objects in an array that are unique with respect to one or more attributes?

这些对象存储在这样的ActiveRecord使用AR的方法将被罚款了。

These objects are stored in ActiveRecord so using AR's methods would be fine too.

推荐答案

使用 阵列#uniq的 与块:

@photos = @photos.uniq { |p| p.album_id }

这篇关于UNIQ在Ruby对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆