2013-02-01 33 views
0

我試圖從我的輪胎搜索結果中排除current_user.id,但無法計算如何將參數傳遞給Tire並使用該參數代替固定值進行搜索。 如何將current_user.id傳遞給模型並將其排除在搜索結果中?輪胎傳遞參數模型和在搜索中使用此參數

ActiveRecord的方式:

Profile.where("user_id != ?", current_user.id) 

我的輪胎搜索塊:

if params['people'] 
    tire.search(load: true, page: params[:page], per_page: 20, :default_operator => 'AND') do 
    query do 
     boolean do 
     must_not { string 'user_id: 1' } <== this works hardcoded 
     must_not { string 'user_id: < instead of 1 user current_user.id pass in true params?' }   
     end 
    end 
    to_curl 
    end 
end 

回答

1

您是否嘗試過使用 「命令行式風格」 的自述中提到的https://github.com/karmi/tire?這是從自述直報價:

search = Tire::Search::Search.new('articles') 
search.query { string('title:T*') } 
search.filter :terms, :tags => ['ruby'] 
search.sort { by :title, 'desc' } 
search.facet('global-tags') { terms :tags, :global => true } 
# ... 
p search.results 

也許「聲明」式摺疊到多塊對方內線,然後在上下文(CURRENT_USER)丟失。你究竟如何在你的布爾條件中插入current_user_id?

+0

current_user.id是Devise gem的一種方法。你不能將它傳遞給Rails模型,唯一能夠獲得id的方法是在表單中使用一個可篡改的隱藏字段,例如user_id。我不明白沒有人似乎有這個問題,不能排除一個特定的ID - 我必須從錯誤的角度解決這個問題 – Rubytastic

+0

這可能是我猜的幫助:http://stackoverflow.com/a/2513456/790737 –

+0

搜索「產卵」如何?從控制器?然後,當從控制器初始化或獲取Rails模型時,您可以讓模型「瞭解」current_user.id。但這一切都取決於搜索是如何「產生」,「開始」的,例如原始搜索來自哪裏(也可能是一個rake任務)。 –