Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails3 jquery autocomplete distinct values

Is it possible to select DISTINCT values using Rails3 JQuery autocomplete gem?

like image 571
thesis Avatar asked Dec 08 '22 00:12

thesis


2 Answers

It is much better and simpler to use scopes.

In your model:

scope :uniquely_named, group(:name)

In your controller:

autocomplete :user, :name, :scopes => [:uniquely_named]

This solution allows more complex filtering to be easily applied:

model:

scope :uniquely_named, group(:name)
scope :online, where(:online => true)

controller:

autocomplete :user, :name, :scopes => [:uniquely_named, :online]

source

like image 158
chech Avatar answered Dec 09 '22 14:12

chech


Doesn't appear to - but - the 'autocomplete' macro is really just defining an action in your controller. I'd forego using 'autocomplete' in my controller and just implement the action myself...

def autocomplete_for_user_name
   User.select('distinct name')
end

Good luck.

like image 27
Cory Avatar answered Dec 09 '22 12:12

Cory