3

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

Vijay Chouhan
  • 4,613
  • 5
  • 29
  • 35

1 Answers1

4

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)  
Philip Whitehouse
  • 4,293
  • 3
  • 23
  • 36
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74