2017-03-22 133 views
1

我有一个现有的轨查询,但它不与mongoid。我的工作需要这样转换,它与mongoid转换轨查询到mongoid查询

这里的工作原理是查询

scope :between, -> (sender_id,recipient_id) do 
    where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id) 
end 

我是新来mongoid我试过,但没能找到合适的解决方案

Mongoid 3.4.2 
Rails 5.0.1 
ruby '2.3.0' 

回答

1

假设的声明是对话模式:

scope :between, -> (sender_id,recipient_id) do 
    any_of({sender_id: sender_id, recipient_id: recipient_id}, {sender_id: recipient_id, recipient_id: sender_id}) 
end 

更新:

使用in运营商,将覆盖您的查询,以及,但将包括sender_id: sender_id, recipient_id: recipient_id不需要的情况下,另一种解决方案。

scope :between, -> (sender_id, recipient_id) do 
    args = [sender_id, recipient_id] 
    where(:sender_id.in => args , :recipient_id.in => args) 
end 

我会选择第一个选项,但第二个可以做的伎俩,以及如果你确定你对价值是每个模型唯一的。

+0

谢谢你的工作 – user6551529