Je hebt het juiste resultaat in beide voorbeelden.
Als u alleen count
gebruikt in select kreeg je altijd een nummer als resultaat van een zoekopdracht. Dus uw resultaat uit de database is te verwachten.
In het geval van rails probeert u een reeks records te krijgen op scope
met count
in select-statement. Het is te verwachten dat de set leeg is als je count
. hebt in uw zoekopdracht.
Probeer count_by_sql
methode http://apidock.com/rails/ActiveRecord/Base/count_by_sql/class om het aantal records te krijgen in plaats van een lege set.
En gebruik het zonder scope, maar met class-methode:
def self.unverified_with_no_associations()
self.count_by_sql("SELECT COUNT(DISTINCT(accounts.id, accounts.email)) FROM accounts WHERE level = 0 AND id NOT IN
(SELECT DISTINCT(account_id) FROM verifications) AND id NOT IN
(SELECT DISTINCT(account_id) FROM positions) AND id NOT IN
(SELECT DISTINCT(account_id) FROM edits) AND id NOT IN
(SELECT DISTINCT(account_id) FROM posts) AND id NOT IN
(SELECT DISTINCT(account_id) FROM reviews) AND id NOT IN
(SELECT DISTINCT(sender_id) FROM kudos) AND id NOT IN
(SELECT DISTINCT(account_id) FROM stacks WHERE account_id IS NOT NULL)")
end