-
Notifications
You must be signed in to change notification settings - Fork 3
search custom method
mmx edited this page Jan 2, 2019
·
1 revision
You can write your own method to retrieve data instead of
autocomplete :client, :name
which actually generates method 'autocomplete_client_name'.
- Create your own method named 'autocomplete_client_name'
def autocomplete_client_name
# your options
model = Client
options = {
display_id: 'id',
display_value: 'name',
limit: 10,
order: 'name ASC'
}
# input
q = params[:q] || ''
# change your query
#where = "your custom query here"
where = ["LOWER(name) LIKE ?", "#{q.downcase}%"] # basic query
limit = autocomplete_option_limit(options)
order = options[:order] || "id ASC"
# get rows from DB
items = model.where(where).order(order).limit(limit)
#
method_display_value = options[:display_value] if options.has_key?(:display_value)
#method_display_value ||= method
method_display_id = options[:display_id] if options.has_key?(:display_id)
method_display_id ||= model.primary_key
data = items.map do |item|
v = item.send(method_display_value)
id = item.send(method_display_id)
[id.to_s, v.to_s]
end
render :json => data.to_json
end