2014-03-31 2 views
0

MongoID를 처음 사용했습니다. 내가 specification/삽입 문서 /의 body 분야에서 $in 연산자를 사용하기 위해 노력하고있어 내 컨트롤러에서

class Vehicle 
    include Mongoid::Document 
    include Mongoid::Timestamps 

    ## 
    # Relationship 
    embeds_one :specification 
end 

class Specification 
    include Mongoid::Document 

    ## 
    # Columns 
    field :body,   type: String 

    ## 
    # Relationship 
    embedded_in :vehicle 

end 

: 나는 다음과 같은 모델을 가지고있다. Embed 문서에서 $in 연산자를 사용하는 방법은 무엇입니까? 코드 아래에서 시도했지만 작동하지 않습니다.

result += Vehicle.where(:specification.matches => { :body.in => param(:body) }) if params[:body].present? 

도움이 :) @로

+0

에 해당하는 MongoDB의 연산자를 사용할 필요가 있습니다 만 'embeds_one'은 임베디드 해시를 둘러싼 다.'Vehicle.where ('specification.body'=> ...)'가 출발점이 될 수 있습니다. –

답변

1

주셔서 감사합니다 MU-IS-너무 짧은 자신의 의견에 언급, 내장 된 문서에서 검색하는 당신이 Vehicle.where(...) 수익을 유의하시기 바랍니다

if params[:body].present? 
    result += Vehicle.where('specification.body' => param(:body)).to_a 
end 

를 사용 a Criteria은 평가할 쿼리의 개체가 아닙니다.

는 또한, in, < 같은 <=을 특정 연산자를 사용하려는 경우 당신은 내가 당신이 뭘 하려는지 모르겠어요 예를

Vehicle.where('specification.body' => {'$in' => param(:body)}) # matches .in 
Vehicle.where('specification.body' => {'$lt' => param(:body)}) # matches < 
Vehicle.where('specification.body' => {'$lte' => param(:body)}) # matches <= 
+0

안녕하세요. @artmees, 대단히 감사합니다. 매력처럼 작동합니다 : D – Zeck