karmi/tire — Issue #182: Best way to filter objects that belong to a user?
Author: schneider-th
Created: 2011-12-23 13:53:19+00:00
State: open

Hi,

I'm using devise and normaly, when I create an array from the database I just call ".where("user_id = ?", current_user.id)" on the model so that the user only gets the objects he owns.

Now I integrated tire and want to do the same thing here. But no matter what I try I either get no results or my app crashes. At the moment my controller function looks like this:

id = current_user.id
@posts = Post.search(params, id)

and the search-method looks like this:

def self.search(params, id)
tire.search(page: params[:page], per_page: 7) do
query { string params[:query], default_operator: "AND" } if params[:query].present?
sort { by :date, "desc" } if params[:query].blank?
filter :terms, :user_id => [id]
end
end

I also tried to make the current_user globally available, but that also didn't work.

I'm sure that somebody of you integrated this to filter objects with the current_user.id so if you could show me your way or tell me what's the best practice I would really appreciate it.

Thanks in regards