Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize list from rails3-jquery-autocomplete

I have a hotel model and its attributes are:

id, hotel_name, address, searchable.

when I set searchable false for particular hotel then this hotel should not come in dropdown when i type in search field.

Controller is

class HotelsController < Admin::BaseController
  autocomplete :hotel, :hotel_name, :full => true
end

How can I optimize this

like image 847
Vijay Chouhan Avatar asked Dec 11 '25 08:12

Vijay Chouhan


1 Answers

In your controller

def get_autocomplete_items(parameters)
  items = active_record_get_autocomplete_items(parameters)
  items = items.where(searchable: true)
end

In the example above we call active_record_get_autocomplete_items rather than super; there is no super as these methods are called dynamically due to the design to handle multiple ORMs. In the above example ActiveRecord is assumed, however Mongoid and MongoMapper are also both currently supported (as others may be in the future). The example above will still work, but depending on the ORM you are using the call to active_record_get_autocomplete_items(parameters) would need to change to match your driver, for example it would become mongo_mapper_get_autocomplete_items(parameters) for the MongoMapper driver.

Using scope

autocomplete :hotel, :hotel_name, :full => true, :scopes => [:searchable_allow]

In model

 scope :searchable_allow, where(searchable: true)  
like image 84
Rajarshi Das Avatar answered Dec 12 '25 22:12

Rajarshi Das



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!