crowdint / rails3-jquery-autocomplete

An easy and unobtrusive way to use jQuery's autocomplete with Rails 3

Home Page:http://rubygems.org/gems/rails3-jquery-autocomplete

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uniqueness

opened this issue · comments

Hi Guys!

I want the queries to be unique as simple as possible.

I want similar search results to not repeat in the list of items.
kDQQi

If there are 100 results of Various Offices and Units, it doesn't look enticing at all.

Is there any way to make it as one?

Here's the gist of my autocomplete-rails.js : https://gist.github.com/xirukitepe/5307204

Controller:

autocomplete :project_procurement_management_plan, :pmo_end_user
Routes:

get "autocomplete_project_procurement_management_plan_pmo_end_user"
Views

<%= autocomplete_field_tag :pmo_end_user, params[:pmo_end_user], autocomplete_project_procurement_management_plan_pmo_end_user_project_procurement_management_plans_path, :class=>"end_user" %>
Any workarounds will be appreciated. Thank you.

I am looking for same thing, Did you discover any workaround ?

For the given example, try this:

class ProjectProcurementManagementPlan < ActiveRecord::Base
    scope :unique_by_pmo_end_user, select(:pmo_end_user).uniq
end

In the controller:

# Auto-complete pmo_end_user in project_procurement_management_plan record.
# The database will have many records with the same pmo_end_user,
# so retrieve from database by distinct pmo_end_user.
# In order for the distinct pmo_end_user selection to work, use the :full_model option to
# trick autocomplete into not selecting specific columns.
autocomplete :project_procurement_management_plan, :pmo_end_user, scopes: [:unique_by_pmo_end_user], full_model: true

@edsimpson 's solution works nicely, but would be nice if a cleaner "right way" were possible

I am closing this issue as uniqueness can be achieved by passing scopes.

This is likely a key feature for many cases... so in the absence of the preferred :uniq => true option, @edsimpson's solution to unique records should absolutely be front-and-center in the README.