ruby on rails - Public activity select unique foreign keys by created time -
i'm looking query activity object in app. i'm using public_activity
gem , have list of activities connected several models , list out recent unique activity.
basically query recent activity particular tracking. let's i'm tracking button , it's id
29
. want query out recent (one) instance of tracking id=29
, other tracking ids. don't know querying rails well. right line have displays activity:
@activities = publicactivity::activity.order("created_at desc").where(owner_id: current_user.id)
let's have table of activities follows:
id activity_id description created_on 1 3 yesterday 2 3 unlike 2 days ago 3 6 comment yesterday 4 7 review yesterday 5 7 review 2 days ago
i want pull out recent entries of each activity id @activities
variable following:
id activity_id description created_on 1 3 yesterday 3 6 comment yesterday 4 7 review yesterday
does public_activity
have workaround this?
you can use combination of group
, select
accomplish this.
publicactivity::activity. select("max(created_at) when, trackable_type"). group("trackable_type"). where(owner_id: current_user.id)
here's output
+-------------------------+-------------------------+ | trackable_type | when | +-------------------------+-------------------------+ | report | 2015-11-27 20:25:31 utc | | quickanswer | 2016-01-04 23:36:21 utc | | comment | 2015-10-23 15:35:32 utc | | takenexam | 2016-01-04 23:46:11 utc | | seenvideo | 2015-08-23 21:27:58 utc | | provider | 2015-09-17 23:47:40 utc | | answerforrecommendation | 2015-08-18 15:13:03 utc | | subscription | 2015-08-24 08:37:08 utc | +-------------------------+-------------------------+
Comments
Post a Comment