I am creating a contest where user can submit multiple entries. Only the entry with the highest tonnage will be shown. In the index view all the entries has to be sorted descending based on tonnage value.
My submissions controller shows following:
@submissions = @contest.submissions.maximum(:tonnage, group: User)
The problem here is that I do not get an array back with all the submission values. I need something I can iterate through.
e.g. a list which only contains one submissions from a user which is the submission with the highest tonnage value.
When I just group I get following error:
GroupingError: ERROR: column "submissions.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "submissions".* FROM "submissions" WHERE "submission...
UPDATE:
I found an sql query who does approximately what I want.
select *
from submissions a
inner join
( select user_id, max(tonnage) as max_tonnage
from submissions
group by user_id) b
on
a.user_id = b.user_id and
a.tonnage = b.max_tonnage
How can I fix this in activerecord?