crowdint / rails3-jquery-autocomplete

An easy and unobtrusive way to use jQuery's autocomplete with Rails 3
http://rubygems.org/gems/rails3-jquery-autocomplete
MIT License
917 stars 367 forks source link

Integrating with MongoId - NoMethodError (undefined method `table_name' for XXX:Class) #252

Closed diderevyagin closed 10 years ago

diderevyagin commented 10 years ago

Hi. Integrating rails3-jquery-autocomplete with mongoid and activeadmin Encountered an error 'NoMethodError (undefined method `table_name' for XXX:Class)'

XXX - model

Model:

class DriverTransaction
  include Mongoid::Document
  include Mongoid::Timestamps
  include SimpleEnum::Mongoid

  attr_accessible :tran_id
  field :tran_id, :type => String
  # other code
end

activeadmin:

collection_action :autocomplete_driver_transaction_tran_id, :method => :get

  controller do
    autocomplete :driver_transaction, :tran_id
  end

  filter :tran_id_contains, :as => :autocomplete, :url => '/admin/driver_transactions/autocomplete_driver_transaction_tran_id',
         :label => "Search", :required => false,
         :wrapper_html => { :style => "list-style: none" }

On changed event send to server call:

Started GET "/admin/driver_transactions/autocomplete_driver_transaction_tran_id?term=XX" for 127.0.0.1 at 2014-02-05 21:55:24 +0200
Processing by Admin::DriverTransactionsController#autocomplete_driver_transaction_tran_id as JSON
  Parameters: {"term"=>"XX"}

On server has error:

Completed 500 Internal Server Error in 6.1ms

NoMethodError (undefined method `table_name' for DriverTransaction:Class):

any ideas and tips - I will be very grateful

diderevyagin commented 10 years ago

Hi. Simple workground: delete:

controller do
  autocomplete :driver_transaction, :tran_id
end 

and define:

  member_action :autocomplete_driver_transaction_tran_id do
    dt = DriverTransaction.any_of({:tran_id => Regexp.new(".*#{params[:term]}.*")})
    dt_arr = []
    dt.each { |cur_dt| dt_arr << cur_dt.tran_id }
    render :json => dt_arr
  end
bigtunacan commented 10 years ago

This sounds like you were able to resolve your own issue?

jeremy6d commented 10 years ago

This gave me a problem when I had a mongoid model that subclassed another mongoid model. The "get_prefix" method that is defined in the autocomplete method in rails3-jquery-autocomplete / lib / rails3-jquery-autocomplete / autocomplete.rb checks the superclass of the object's class and mandates it be "Object" in order for the prefix to be correctly selected. So if you subclass you break this.

The only way I've been found to workaround this is to monkey patch the autocomplete method to return "mongoid" in the get_prefix definition.