I'm trying to group a table (oauth_access_tokens) by application_id and select the highest record by ID from the corresponding group (see the complete model below). I've seen this post, which explains how to get the highest ID from the group, but sadly it's not working for my case.
I have a table called oauth_access_tokens with the following attributes: id, resource_owner_id, application_id, token, refresh_token, expires_in, scopes and revoked_at.
The method I have in my model:
def last_token
Doorkeeper::AccessToken.group(:application_id).having('id = MAX(id)')
end
After calling it like so: User.first.last_token (I made sure that there are a few records in the database present).
sql output:
Doorkeeper::AccessToken Load (0.5ms) SELECT `oauth_access_tokens`.* FROM `oauth_access_tokens` GROUP BY `oauth_access_tokens`.`application_id` HAVING id = MAX(id)
Output: #<ActiveRecord::Relation []>
Why don't I get the record with the highest ID? When I run User.first.last_token I expect to see the access_token with the id of 28.
Happy holidays!
