Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 3-上的where子句通过关联具有多个_Ruby On Rails_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails Rails 3-上的where子句通过关联具有多个

Ruby on rails Rails 3-上的where子句通过关联具有多个,ruby-on-rails,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby On Rails 3,Activerecord,你好, 我有一个关于where查询的小问题:通过关联 我的设置如下: PurchaseOrderAddressAssignment: belongs_to :address belongs_to :purchase_order has_many :purchase_order_address_assignments has_many :purchase_orders, :through => :purchase_order_address_assignments has_many

你好,

我有一个关于where查询的小问题:通过关联

我的设置如下:

PurchaseOrderAddressAssignment:

belongs_to :address

belongs_to :purchase_order
has_many :purchase_order_address_assignments

has_many :purchase_orders, :through => :purchase_order_address_assignments
has_many :purchase_order_address_assignments

has_many :addresses, :through => :purchase_order_address_assignments
PurchaseOrder.where("addresses.id = 168 and addresses.id = 169").includes(:addresses)
地址:

belongs_to :address

belongs_to :purchase_order
has_many :purchase_order_address_assignments

has_many :purchase_orders, :through => :purchase_order_address_assignments
has_many :purchase_order_address_assignments

has_many :addresses, :through => :purchase_order_address_assignments
PurchaseOrder.where("addresses.id = 168 and addresses.id = 169").includes(:addresses)
采购订单:

belongs_to :address

belongs_to :purchase_order
has_many :purchase_order_address_assignments

has_many :purchase_orders, :through => :purchase_order_address_assignments
has_many :purchase_order_address_assignments

has_many :addresses, :through => :purchase_order_address_assignments
PurchaseOrder.where("addresses.id = 168 and addresses.id = 169").includes(:addresses)
我的where子句:

belongs_to :address

belongs_to :purchase_order
has_many :purchase_order_address_assignments

has_many :purchase_orders, :through => :purchase_order_address_assignments
has_many :purchase_order_address_assignments

has_many :addresses, :through => :purchase_order_address_assignments
PurchaseOrder.where("addresses.id = 168 and addresses.id = 169").includes(:addresses)
正在返回0条记录。。。但至少应该有1个

PurchaseOrder.where(:baan_id => "KD0005756").first.address_ids
正在返回[168169170327]

。。。我觉得我太笨了,解决不了这个小问题:-/

有人能告诉我我做错了什么吗

Thx


Michael

所以你想要
采购订单
,其中
地址
在某个id的列表中

试试这个:

PurchaseOrder.where(addresses: {id: [168, 169]}).includes(:addresses)

在这种情况下,我可能会使用自定义查找器方法

class PurchaseOrder < ActiveRecordBase
  def self.with_addresses(*args)
    values = args.flatten.uniq

    # Note use :joins instead of :includes if you don't
    # want the addresses data
    includes(:addresses)
    where(:addresses => {:id => values})
    group("purchase_orders.id")
    having("count(ad‌​dresses.id)=#{values.size}")
  end
end
class PurchaseOrder{:id=>values})
组(“采购订单.id”)
有(公元‌​dresses.id)=#{values.size}”)
结束
结束
我很确定这应该行得通


下面是一个有助于解释查询的示例。

尝试更改此
PurchaseOrder.where(“addresses.id=168和addresses.id=169”)。包括(:addresses)
PurchaseOrder.where(addresses.id=>[168169])。包括(:addresses)
并查看发生了什么。thx以获得您的响应。我忘了说我已经用IN(…)试过where子句了。这里的问题是,如果我使用where(“addresses.id IN(n1,n2,n3)”),查询将返回大约1000个PurchaseOrder。。。在我的场景中,只有一个PurchaseOrder的地址为[168,169,327]
addresses.id=>[168169]
应仅限于这两个。
PurchaseOrder.where(“addresses.id”=>[168169327])。包括(:addresses)。count
返回972<代码>采购订单。其中(“addresses.id=327”)。包括(:addresses)。计数返回1。我一直认为第一个查询应该返回与第二个查询相同的内容。。。我有点困惑:-/我需要得到第二个查询中的值。我的道歉我被甩了。。。你需要的是168、169和327的那一个?问题是它将包含所有具有168或169的
PurchaseOrder
s,而不仅仅是包含这些地址的
PurchaseOrder
。Thx Azolo,这正是我要找的!