-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option to remove duplicate results ? #45
Comments
@alaouy For something like that you should filter using a scope and then use the autocomplete scopes option. |
I tried to remove duplicate results with a scope but failed. Here's my case:
I want autocompleting for all names but I do not want duplicated names. I used
But it generate SQL like following:
Anyone can help? |
I end up with a patch to ActionController::Base class ActionController::Base
# Unique feature for rails-jquery-autocomplete
def self.autocomplete_uniq(object, method, options = {})
define_method("autocomplete_#{object}_#{method}") do
model = (options[:class_name] || object).to_s.camelize.constantize
sql = model.select("MIN(id) as id, #{method} as label, #{method} as value").group(method).
where("#{method} like ?", "%#{params[:term]}%").limit(10).to_sql
data = model.connection.select_all(sql)
render json: data
end
end
end So I can use following code to query unique result class Admin::CaseSearchesController < Admin::AdminController
autocomplete_uniq :option, :name, class_name: 'Consult::Option'
end |
Aha! Maybe I should use 'unscope' or with class Item < ActiveRecord::Base
scope :uniq_brand, -> { select('MIN(id) as id, brand').group(:brand) }
end
class Admin::CaseSearchesController < Admin::AdminController
autocomplete :item, :brand, full: true, scopes: [:uniq_brand], full_model: true
end The implement is a little tricky. |
Scopes can also override the select and where. So you can use code like following to query unique result. autocomplete :item, :brand, full: true, scopes: [-> { unscope(:select).select('MIN(id) as id, brand').group(:brand) }]
I finally add a unique option. So you can just use code like following to query unique result:
See details here, and change your Gemfile if you can not wait the PR.
|
@bigtunacan @lingceng I'm using MongoDB, and you can't do |
@alaouy would it be possible to post a basic public sample project illustrating this? It would help me with looking into a solution for this since I have only used Mongo minimally. |
Is there an option to remove duplicate results ?
The text was updated successfully, but these errors were encountered: