2012-09-22 2 views
0

내가 Mongoid 3.Mongoid가 포함 된 문서

그래서 (가짜 예) 상상 내가 가진를 사용하여 일부 포함 docuements에 두 쿼리의 교회법을 얻으려고에 기준을 교차 난 그냥

Post.where(:'comments.author_id' => User.first.id) 

그러나 나는 이러한 사용자의 모두 의견이 게시물을 얻으려면을 할 수있는 사용자로부터 의견에 게시물을 얻고 싶었다 :

u1 = User.first.id 
u2 = User.last.id 
Post.where(:'comments.author_id' => u1.id, :'comments.author_id' => u2.id) 

이 같은 것을 얻을 수 있도록이 그것은 두 번째로 첫 번째 comments.author_id을 덮어 mongoid 3에서 작동하지 않습니다 : 나는 운없이 시도했습니다

command={:count=>"posts", :query=>{"comments.author_id"=>"505d1eb5f8182b7082000017"}} 

다른 변형을 :

Post.where(:'comments.author_id' => u1.id).where(:'comments.author_id' => u2.id) 
Post.where(:'comments.author_id' => u1.id).and.where(:'comments.author_id' => u2.id) 
Post.where(:'comments.author_id' => u1.id).intersect.where(:'comments.author_id' => u2.id) 
Post.all_of(:'comments.author_id' => u1.id, :'comments.author_id' => u2.id) 

이 쿼리를 구성하는 더 좋은 방법이 있습니까? 감사!

답변

1

여기에서 문제는 Mongoid가 메소드를 실행하기 전에 Ruby 해시를 제공 했으므로 Mongoid에서 아무 것도 할 수 없다는 것입니다. 키가) 동일하기 때문에 :

Post.where ('comments.author_id'=> u1.id, 'comments.author_id'=> u2.id)

은 무엇 당신이 원하는 것은 :

Post.any_in ('comments.author_id'=> [u1.id, u2.id])

+0

아 그래, 난하지 않았다 믿을 수 없어 이것 생각 해봐! 답변 해주셔서 감사합니다. 관련 질문 : 1 명의 저자가 아닌 다른 사람의 의견이있는 게시물을 얻을 수 있습니까? ': 'comments.author_id'.ne => u2.id'와 같은 정렬을 두 번째로합니까? –