I can get a collection of accounts with multiple users:
Account.group('accounts.id HAVING count(users.id) > 1').joins(:users)
But, as soon as I call .count on this object, I get a huge explosion:
(0.3 ms) SELECT COUNT (*) AS count_all, accounts.id HAVING count (users.id)> 1 AS accounts_id_having_count_users_id_1 FROM "accounts" INNER JOIN "users" ON "users". "account_id" = "accounts". "id" GROUP BY accounts.id HAVING count (users.id)> 1 ActiveRecord :: StatementInvalid: PG :: Error: ERROR error: syntax error at or near "AS" LINE 1: ... unt_all, accounts. id HAVING count (users.id)> 1 AS accounts ...
It seems that in postgres the actual request I want:
select count(*) from (SELECT accounts.id FROM "accounts" INNER JOIN "users" ON "users"."account_id" = "accounts"."id" GROUP BY accounts.id HAVING count(users.id) > 1) as a;
How can I get activerecord to create this (or comparable) query?
source
share