协会下面是一些背景的小数据库架构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' –