I have an ActiveRecord relation for a many-to-many relationship and I would like to return a collection of the unique items from one side of the relationship.
Ideally, I would like to do this together, but I'm aware this might not be possible.
The key thing I'm trying to do is remove duplicates based on the foreign key, without losing the additional data by calling select(:foreign_key).distinct
and while preserving the ActiveRecord_Relation
type to allow further queries.
As an example:
Article.first.comments
returns 10 comments from 6 commenters (alias for users). Article.first.comments.select(:commenter_id).distinct
returns the 6 commenters but loses the time stamps.
I want a way to get all the commenters for the Article ordered by when they left the comment.