Skip to content
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

Open
alaouy opened this issue Jan 13, 2016 · 7 comments
Open

Option to remove duplicate results ? #45

alaouy opened this issue Jan 13, 2016 · 7 comments

Comments

@alaouy
Copy link
Contributor

alaouy commented Jan 13, 2016

Is there an option to remove duplicate results ?

capture d ecran 2016-01-13 a 12 29 59

@bigtunacan
Copy link
Collaborator

@alaouy For something like that you should filter using a scope and then use the autocomplete scopes option.

@lingceng
Copy link

lingceng commented Feb 3, 2016

I tried to remove duplicate results with a scope but failed.

Here's my case:

name category
Jack A
Jack B
Tom A
Jim B
Tom C

I want autocompleting for all names but I do not want duplicated names.

I used distinct.

class Consult::Option < ActiveRecord::Base
  scope :no_dup, -> { distinct }
end

class Admin::CaseSearchesController < Admin::AdminController
  autocomplete :option, :name, full: true, class_name: 'Consult::Option', scopes: [:no_dup]
end

But it generate SQL like following:

SELECT  DISTINCT consult_options.id, consult_options.name FROM `consult_options` WHERE `consult_options`.`deleted_at` IS NULL AND (LOWER(consult_options.name) LIKE '%qin%')  ORDER BY LOWER(consult_options.name) ASC LIMIT 10

Anyone can help?

@lingceng
Copy link

lingceng commented Feb 4, 2016

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

@lingceng
Copy link

lingceng commented Feb 4, 2016

Aha! Maybe I should use 'unscope' or with full_model:true option.
Something like following:

    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.
Maybe it's better when scopes can accept a Proc and can override the selects and wheres.
And a option to remove duplicate is useful too.

lingceng added a commit to lingceng/rails-jquery-autocomplete that referenced this issue Feb 5, 2016
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) }]
@lingceng
Copy link

lingceng commented Feb 5, 2016

I finally add a unique option. So you can just use code like following to query unique result:

 autocomplete :item, :brand, full: true, unique: true

See details here, and change your Gemfile if you can not wait the PR.

 gem 'rails-jquery-autocomplete', github: 'lingceng/rails-jquery-autocomplete'

@alaouy
Copy link
Contributor Author

alaouy commented Feb 8, 2016

@bigtunacan @lingceng I'm using MongoDB, and you can't do distinct on multiple fields in MongoDB, that's why having an option that does that with some kind of filter on the resulting dataset would be very useful.

@bigtunacan
Copy link
Collaborator

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants