ActiveRecord: unique attribute

I am trying to filter ActiveRecord_AssociationRelation

to be unique by parent id.

So, I want a list like this:

[#<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">]}

      

to get this back:

[#<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">]

      

I have tried various methods including:

@messages.uniq(&:parent_id) # returns the same list (with duplicate parent_ids)

@messages.select(:parent_id).distinct # returns [#<Message id: nil, parent_id: 20>]

      

and uniq_by

have been removed from Rails 4.1.

+3


source to share


2 answers


You tried

group(:parent_id)

      



It seems to me that this is what you need. This returns the first record with the specified parent_id. If you want to use the last record, you will have to reorder the result in the subquery and then use the group.

+3


source


For me in Rails 3.2 and Postgresql, Foo.group (: bar) works with simple queries, but gives me an error if I have any suggestions there, like

 irb> Message.where(receiver_id: 434).group(:sender_id)
 => PG::GroupingError: ERROR:  column "messages.id" must appear in the
    GROUP BY clause or be used in an aggregate function

      

I ended up specifying the "DISTINCT ON" SQL clause for the selection. In the Message class, I have the following scope:



 scope :latest_from_each_sender, -> { order("sender_id ASC, created_at DESC").select('DISTINCT ON ("sender_id") *') }

      

Using:

 irb> Message.where(receiver_id: 434).latest_from_each_sender

      

+3


source







All Articles