Rails3 jquery autocomplete various values

Can I select DISTINCT values ​​using Rails3 jQuery autocomplete?

+4
source share
5 answers

It is much better and easier to use areas.

In your model:

scope :uniquely_named, group(:name) 

In your controller:

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

This solution makes it easy to apply more complex filtering:

Model:

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

controller:

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

a source

+9
source

Not displayed, but the "autocomplete" macro really defines the action in your controller. I would refuse to use "autocomplete" in my controller and just implement the action myself ...

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

Good luck.

+3
source

I try this:

 class UbicationsController < ApplicationController autocomplete :ubication, :name def autocomplete_ubication_name render json: Ubication.select("DISTINCT name as value").where("LOWER(name) like LOWER(?)", "%#{params[:term]}%") end end 
+2
source

None of the other answers work with MongoID.

I use a lot of answers for this:

 class ProductsController < ApplicationController autocomplete :product, :manufacturer autocomplete :product, :model def autocomplete_product_manufacturer render_autocomplete(:manufacturer) end def autocomplete_product_model render_autocomplete(:model) end def render_autocomplete(field) render json: Product.where(field => /#{params[:term]}/i).where(domain: @domain).distinct(field) end end 

It does not use a relationship. The manufacturer and model are the row field on the product model.

Thanks everyone for the answers.

+1
source

Postgresql:

  scope :uniquely_named, :select => 'distinct name' #In model autocomplete :user, :name, :scopes => [:uniquely_named] #In controller 
0
source

Source: https://habr.com/ru/post/1332611/


All Articles