協會下面是一些背景的小數據庫架構ActiveRecord的 - 查詢與多個條件對
Order
-> id
LineItem
-> id
-> order_id
Transaction
-> id
-> transaction_type
-> successful
-> line_item_id
基本上,我們正在試圖做的是獲取具有多個交易的所有訂單,在符合特定條件對交易
這裏是作爲一個例子來使用一個非常基本的表模式:
Order (orders)
+----+
| id |
|----|
| 1 |
| 2 | <--
| 3 |
Line Items (line_items)
+---------------+
| id | order_id |
|----|----------|
| 1 | 1 |
| 2 | 2 | <--
| 3 | 3 |
Transactions (transactions)
+---------------------------------------------------+
| id | transaction_type | successful | line_item_id |
|----|------------------|------------|--------------|
| 1 | 2 | 1 | 1 |
| 2 | 2 | 0 | 2 | <--
| 3 | 1 | 1 | 1 |
| 4 | 3 | 1 | 2 | <--
| 5 | 1 | 1 | 3 |
| 6 | 3 | 1 | 3 |
我用箭頭突出顯示了我們將關注的記錄。
的Order
類把更多的上下文:
class Order < ActiveRecord::Base
has_many :line_items
has_many :transactions, through: :line_items
end
正如我所說的,我們希望在transactions
獲取通過特定的約束條件(對)的所有訂單。
所以我們只希望選擇一個order
如果我們有(At least ONE transaction with "transaction_type" = 2 AND "successful = 0"), AND (At least ONE transaction with "transaction_type" = 3 AND "successful" = 1)
這不能是OR
。只有在遵守兩個約束條件的情況下才能選擇順序。
(例如:我只想要訂單,其中有訂單項只有一個類型2的交易AND不成功)。 [這應該只給我訂單,有單一類型的交易,但它可以有任何其他類型的交易]
我一直在搜索最近10小時如何實現這一點,無濟於事,所以我轉向你尋求答案。 如果您好奇:我們使用postgres。
我甚至不知道從哪裏開始查詢,甚至不知道ActiveRecord語法。
問題的好例子: https://github.com/dsounded/ransack_example/blob/master/app/services/finder.rb#L7 問題在於無法命令或分頁結果。
這樣的查詢結果:' SELECT COUNT(*)FROM「transactions」INNER JOIN transactions as x ON x.line_item_id = transactions.line_item_id WHERE(transactions.transaction_type = 2 AND transactions.successful ='f')AND(x.transaction_type = 3 AND x.successful = 't')' –
成功fie的類型是什麼在數據庫中的ld? –
這是布爾值。 'f'|| 't' –