我想通過父ID來過濾ActiveRecord_AssociationRelation
s。ActiveRecord:按屬性唯一
所以,我想這樣的名單:
[#<Message id: 25, posted_by_id: 3, posted_at: "2014-10-30 06:02:47", parent_id: 20, content: "This is a comment", created_at: "2014-10-30 06:02:47", updated_at: "2014-10-30 06:02:47">,
#<Message id: 23, posted_by_id: 3, posted_at: "2014-10-28 16:11:02", parent_id: 20, content: "This is another comment", created_at: "2014-10-28 16:11:02", updated_at: "2014-10-28 16:11:02">]}
返回此:
[#<Message id: 25, posted_by_id: 3, posted_at: "2014-10-30 06:02:47", parent_id: 20, content: "This is a comment", created_at: "2014-10-30 06:02:47", updated_at: "2014-10-30 06:02:47">]
我已經試過各種方法,包括:
@messages.uniq(&:parent_id) # returns the same list (with duplicate parent_ids)
@messages.select(:parent_id).distinct # returns [#<Message id: nil, parent_id: 20>]
和uniq_by
已從Rails 4.1中移除。
感謝,這正是我想要的。 – user3281384 2014-11-05 00:50:55