链接Mongoid标准

时间:2014-05-18 19:01:01

标签: ruby-on-rails ruby mongodb mongoid mongoid3

我试图将mongoid标准联系起来。

如果我获取 "$in"(或:field.inModel.any_in)条件,我会得到预期结果。

当我像下面的例子中那样组合它们时,我的结果是不可取的。

  • 如何将它们链接到AND查询?
    我的结果应该返回
  • 有IN标准的人。

rSpec示例

@person = Fabricate :person, categories: [@c1], services: [@s1]
@person2 = Fabricate :person
get 'person_search', categories: "#{@c1.id}", services: "#{@s1.id}"
expect(assigns(:person)).to include(@person)
expect(assigns(:person)).to_not include(@person2)

。在

query = Person.where(country: "AT")

query = query.in(:category_ids => categories.collect(&:_id)) if categories.present?
query = query.in(:service_ids => services.collect(&:_id)) if services.present?

@people = query.map do |person|
  person
end

我也试过

query = Person.where(country: "AT")

query = query.where(:category_ids.in => categories.collect(&:_id)) if categories.present?
query = query.where(:service_ids.in => services.collect(&:_id)) if services.present?

@people = query.map do |person|
  person
end

这也不起作用:

query = Person.where(country: "AT")

query = query.all(:category_ids.in => categories.collect(&:_id)) if categories.present?
query = query.all(:service_ids.in => services.collect(&:_id)) if services.present?

@people = query.map do |person|
  person
end

0 个答案:

没有答案