I have a scope that joins users to posts, to get only users that have visible posts. This works in MySQL, but PG is a lot more strict, and throws an error.
User Model:
belongs_to :account
scope :have_posts, joins(:posts).where('posts.visible => true').group('users.id')
Controller:
@account.users.have_posts.each do |user|
# do stuff
end
Error:
(PGError: ERROR: column "users.account_id" must appear in the GROUP BY clause or be used in an aggregate function: SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_id" = "users"."id" WHERE ("users".account_id = 1) AND (recommendations.approved = true) GROUP BY users.id)
It's complaining about "users.account_id" which is from calling @account.users (as I obviously don't want all users in the DB).
Any idea how to fix?