I have a model called Event
which belongs to Area
and Task
. I'm attempting to retrieve a collection of events that only contains the most recent event per area and task combination. That is, I only want the most recent event of the events that have the same area_id and task_id. Example collection of events:
|event_id|area_id|task_id| ... |
|--------|-------|-------|-----|
| 5 | 3 | 2 | ... |
| 4 | 3 | 1 | ... |
| 3 | 3 | 2 | ... |
Here I want only event 5 and 4 to be returned since 3 is older.
I've tried using Event.select(:area_id,:task_id).distinct
which seems to work, but strips all other attributes of the returned events, including :id
. Grateful for any help or suggestions!