Gem 关于 Mongoid 查询时 any_of 的合并问题

Elvis · December 13, 2011 · 3228 hits

两个 scope 里都有用到 any_of,似乎在一起调用的这两个 scope 的时候,两个 any_of 里的条件会全部 or 起来。。。

小生遇到的情况是这样,在 Message 类里写了两个 scope

scope :conversations, lambda { |parent_id| any_of({ parent_message_id: parent_id }, { _id: parent_id }) }
scope :undelete, lambda{ |user_id| any_of({:user_id => user_id, :user_deleted_at => nil}, {:to_user_id => user_id, :to_user_deleted_at => nil}) }

然后在调用 Message.conversations(87).undelete(15) 的时候,生成的 query 变成了

=> #<Mongoid::Criteria
  selector: {"$or"=>[{:parent_message_id=>87}, {:_id=>87}, {:user_id=>15, :user_deleted_at=>nil}, {:to_user_id=>15, :to_user_deleted_at=>nil}]},
  options:  {:sort=>[[:_id, :desc]]},
  class:    Message,
  embedded: false>

两个 scope 里的条件都 or 到一起去了,而我的本意是在第一条结果的子集里面再去查后一条,不知应如何修改。。

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.