2016-06-14 22 views
1

我有一個難題。我在我的Account類中編寫了一個範圍,該類找到了一堆滿足冗長條件的帳戶對象。這裏是我的範圍:SQL查詢和ActiveRecord.find_by_sql返回不同結果的可能性如何?

scope :unverified_with_no_associations, -> { 
    find_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)") 
} 

當我執行與Account.unverified_with_no_associations這個範圍我收到這個對象返回[#<Account:0x007f7fc94d79c0 id: nil>]

然而,當我連接到數據庫,併爲是執行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); 

我收到的號碼221214。爲什麼會得到不同的結果?我排除了連接到不同數據庫的可能性。我檢查了我的.env文件,並確認我與我的應用程序位於同一個數據庫中。有誰知道我爲什麼會在查詢中獲得這樣的差異?

------------ -------- UPDATE

我發現find_by_sql不喜歡的COUNT在其參數納入。當我從sql中刪除COUNT()並稍後執行.count方法時,我檢索匹配的數字。

但是,當我使用這兩種方法時,我仍然得到不同的結果。

我真正需要的是不同的accounts.idaccounts.email但這些方法不會返回相同的輸出。

例如,當我執行的SQL版本我收到的輸出,看起來像這樣:

row 
--------- 
(1234,[email protected]) 

但是當我使用ActiveRecord的版本,我得到這個:

[#<Account:0x007fdc9ec104d0 id: nil>] 

,但沒有隨郵件一起。

----更新#3 ------

而且在我的SQL輸出我得到這個unknown OID 2249: failed to recognize type of 'row'. It will be treated as String.這是什麼意思?

回答

1

你在這兩個例子中都有正確的結果。

如果您在select中僅使用count,則始終會收到一個作爲查詢結果的數字。所以你的數據庫結果是可以預期的。

在rails案例中,您試圖通過scopecount在select語句中獲得一些記錄集。如果您的查詢中有count,那麼可以期待得到空集。

嘗試count_by_sql方法http://apidock.com/rails/ActiveRecord/Base/count_by_sql/class 獲取記錄數而不是空集。

而且使用它沒有範圍,但類方法:

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 
+0

rootatdarkstar,感謝您的回覆。我不知道有一個'count_by_sql'方法。你會知道我在更新中指定的問題嗎?我仍然沒有匹配的記錄。 –

+0

我會將您的答案標記爲正確並打開一個新問題。感謝您對'count_by_sql'的幫助。 –

+0

無論如何,儘量使用你的sql代碼*沒有*範圍。 – rootatdarkstar