diff --git a/lib/rails-jquery-autocomplete/orm/active_record.rb b/lib/rails-jquery-autocomplete/orm/active_record.rb index 9348606..93d3c72 100644 --- a/lib/rails-jquery-autocomplete/orm/active_record.rb +++ b/lib/rails-jquery-autocomplete/orm/active_record.rb @@ -29,6 +29,13 @@ def active_record_get_autocomplete_items(parameters) limit(limit).order(order) items = items.where(where) unless where.blank? + if options[:unique] + scopes << lambda { + select = "MIN(#{table_name}.#{model.primary_key}) as #{model.primary_key}, #{table_name}.#{method}" + unscope(:select).select(select).group(method) + } + end + scopes.each do |scope| items = case scope when String @@ -42,16 +49,17 @@ def active_record_get_autocomplete_items(parameters) end def get_autocomplete_select_clause(model, method, options) - if sqlite? + base = if sqlite? table_name = model.quoted_table_name - ([ - "#{table_name}.#{model.connection.quote_column_name(model.primary_key)} as #{model.primary_key}", - "#{table_name}.#{model.connection.quote_column_name(method)} as #{method}" - ] + (options[:extra_data].blank? ? [] : options[:extra_data])) + [ + "#{table_name}.#{model.connection.quote_column_name(model.primary_key)} as #{model.primary_key}", + "#{table_name}.#{model.connection.quote_column_name(method)} as #{method}" + ] else table_name = model.table_name - (["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + (options[:extra_data].blank? ? [] : options[:extra_data])) + ["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] end + base + (options[:extra_data] || []) end def get_autocomplete_where_clause(model, term, method, options)