ruby-on-railsrubyruby-on-rails-3jquery-autocompletenamed-scope

Rails gem rails3-jquery-autocomplete how to scope by user


I'm using the Rails gem rails3-jquery-autocomplete to add categories to posts.

I would like to restrict the search to include only categories that belong to the current user or post's author in the results.

The documentation says that I can specify a scope:

:scopes

Added option to use scopes. Pass scopes in an array. e.g :scopes => [:scope1, :scope2]

But I'm not sure how I would pass the user id here?

It seems like a comon scenario, am I missing something obvious?

I found an answer that suggests modifying the get_item method, but that seems to break the auto-complete

Scoping the results for rails3 jquery autocomplete plugin


Solution

  • In posts_controller:

     def get_autocomplete_items(parameters)
        items = super(parameters)
        items = items.where(:user_id => current_user.id)
      end
    

    I'm first calling the original get_autocomplete_items method, and then filtering out the results by current_user.id.

    This question helped: Rails 3: alias_method_chain still used?