0

Im trying to exclude current_user.id from my Tire search results but cannot figure how to pass a param to Tire and use that instead of a fixed value for search. How could one pass the current_user.id to the model and exclude it in search results?

Activerecord way:

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

My tire search block:

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
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Rubytastic
  • 15,001
  • 18
  • 87
  • 175

1 Answers1

1

Have you tried to use the "imperative style" the README mentions on https://github.com/karmi/tire ? This is a straight quote from the readme:

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

Maybe the "declarative" style folds to much blocks inside each other, and then the context (current_user) is lost. How exactly did you insert the current_user_id in your boolean condition?

Sjors Branderhorst
  • 2,138
  • 17
  • 25
  • current_user.id is a method by Devise gem. You cannot pass this to a Rails model, the only way im able to get the id is to use a tamperable hidden field in the form for example called user_id. I don't understand nobody seems to have this issue of not be able to exclude a certain id - I must tackle this problem from the wrong angle – Rubytastic Feb 05 '13 at 14:54
  • How is the search "spawned". From a controller? Then when initializing or fetching the Rails model from your controller you could let the model "know" about the current_user.id . But it all depends on how the search is "spawned", "started", e.g. where the original search comes from (could also be a rake task). – Sjors Branderhorst Feb 07 '13 at 15:52
  • thx for the suggestions. its called from view that posts the form to controller. Havent been able to fix this, your provided suggestion could be a solution but its very messy and goes against the rails best practice ( not mixing controller stuff in the models ) – Rubytastic Feb 07 '13 at 16:35