Reputation: 75
I'm trying to upload my app to heroku but it gives me an error in this statement:
@exhibition.enrolments.select(:id).group('payment_id')
ActiveRecord::StatementInvalid (PG::GroupingError: ERROR: column "enrolments.id" must appear in the GROUP BY clause or be used in an aggregate function
In SQlite I didn't have any errors, but in Production I have it.
The question is: I have this:
enrolment_id | payment_id | ....
1 | 1 | ....
2 | 1 | ....
3 | 2 | ....
4 | 3 | ....
5 | 3 | ....
And I want to get the enrolments' ids 2,3 and 4.
I tried this too:
@exhibition.enrolments.select(:id).group('enrolment.id, payment_id')
It works but it just gives me all entries, obviously...
I read to many posts about this but I didn't get the solucion...
Any help? Thank you.
EDIT:
I mean:
And I want to get the enrolments' ids 2,3 and 5.
The greatest enrolment_id in each block of payments with the same id.
Thx Frank Heikens
Upvotes: 0
Views: 74
Reputation: 4877
So give it the aggregate function it wants?
@exhibition.enrolments.select(:id).group('payment_id').max(:id)
Alternatively, you could order the records and use distinct
@exhibition.enrolments.order('payment_id DESC').select(:payment_id).distinct
Upvotes: 1