Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save patrickberkeley/164997 to your computer and use it in GitHub Desktop.
Save patrickberkeley/164997 to your computer and use it in GitHub Desktop.
module AutoCompleteJquery
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def auto_complete_association_for(object, klass, attribute, options = {})
define_method("auto_complete_for_#{object}_#{klass}_#{attribute}") do
klass_constant = klass.to_s.camelize.constantize
@items = klass_constant.connection.select_values(%Q[
SELECT #{attribute}
FROM #{klass_constant.table_name}
WHERE #{attribute}
LIKE '%#{params[:q].downcase}%'
LIMIT 10
])
render :text => @items.join("\n")
end
end
end
end
class ItemsController < ApplicationController
auto_complete_association_for :item, :category, :name
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment