-
Notifications
You must be signed in to change notification settings - Fork 4
search where
mmx edited this page Jan 2, 2019
·
1 revision
- add additional filter using :where option
autocomplete :client, :name, { :where => 'region_id=1' }
it will select clients only from 'region_id=1'. If user searches for text 'ab' in autocomplete field it will query the database:
SELECT clients.id, clients.name FROM "clients" WHERE (LOWER(clients.name) LIKE 'ba%') AND (region_id=1) ORDER BY clients.name ASC LIMIT ? [["LIMIT", 10]]
additional filter using :scopes option
autocomplete :client, :name, { :scopes => [:w_usa, :w_active]}
- define scopes in model
class Client < ActiveRecord::Base
...
# scopes
scope :w_usa, -> { where(region_id: 1) }
scope :w_active, -> { where(active: true) }
end
use :where_method option to add dynamic search conditions.
class ClientsController < ApplicationController
autocomplete :client, :name, { :where_method => :w_region }
def w_region
# get value from params
v = params[:region_id]
if v
w = "region_id=#{v}"
else
w = nil
end
w
end
end
it will search data depending on value in params[:region_id].