I'm trying to order a group of TV shows by how many times an actor has been on. I followed some other stack overflow questions asking the same question but I'm getting an error I don't see mentioned anywhere.
Show.joins(:contributions)
.select('show.*, COUNT(contributions.id) AS guest_count')
.where('contributions.role_id = 2')
.where('contributions.person_id IN (?)', self.id)
.order('guest_count desc')
PG::SyntaxError: ERROR: syntax error at or near "AS" LINE 1: SELECT COUNT(show.*, COUNT(contributions.id) AS guest_co...
Output:
: SELECT COUNT(COUNT(contributions.id) AS guest_count, show.*) FROM
"show" INNER JOIN "episodes" ON "episodes"."show" = "show"."id"
INNER JOIN "contributions" ON "contributions"."episode_id" =
"episodes"."id" WHERE (contributions.role_id = 2) AND
(contributions.person_id IN (42))
This is the stack overflow I've been following: Rails 3 ActiveRecord: Order by count on association