0

I am using rails3-jquery-autocomplete plugin.

I would like to search by several attributes, for instance title and app_number.

What I tried:

in model:

class Eclaim < ActiveRecord::Base
  scope :search_by_number, ->(query){ 
      (query ? where(["app_number LIKE ?", '%'+ query + '%']) : {})
  }
end

in controller:

class ClaimsController < ApplicationController
   autocomplete :eclaim, :title,  :extra_data => [:app_number], :scopes => [:search_by_number]
end

But when I am using auto complete field I get error:

ArgumentError (wrong number of arguments (0 for 1)):
  app/models/eclaim.rb:16:in `block in <class:Eclaim>'

It looks like the plugin doesn't assing query variable.

Can Anyone help me?

Lesha Pipiev
  • 3,251
  • 4
  • 31
  • 65

1 Answers1

3

hope this helps: Rails gem rails3-jquery-autocomplete: How do I query multiple fields

That error means Eclaim.search_by_number() was called without any arguments. Rather curious indeed. Let me know if the link above assists you in solving this.

Note that :extra_data is only setting extra attributes you want returned in the autocompleter results via the JSON response.

Update: Try this in your controller, adjusting for :term

def get_autocomplete_items(parameters)
   super(parameters)
   items = Eclaim.search_by_number(params[:term])
end
Community
  • 1
  • 1
Michael De Silva
  • 3,808
  • 1
  • 20
  • 24
  • Thanks, I have seen it )) If I do **Eclaim.search_by_number('myquery')** in console it works. But it doesn't work automatically with autocomplete plugin. Maybe I don't understand something? What Can I do with **Eclaim.search_by_number('myquery')**? – Lesha Pipiev Jun 20 '12 at 16:44
  • Maybe Should I play around redefine **get_autocomplete_items** method? – Lesha Pipiev Jun 20 '12 at 16:46
  • Take a look at http://stackoverflow.com/questions/7244515/rails-gem-rails3-jquery-autocomplete-how-to-scope-by-user You may need to override `get_autocomplete_items(parameters)` in your controller. – Michael De Silva Jun 20 '12 at 16:50
  • Also this http://stackoverflow.com/questions/4858816/scoping-the-results-for-rails3-jquery-autocomplete-plugin It seems like the way to go, you can then do `items = Eclaim.search_by_number(params[:foo])` making sure you adjust for `:foo` =) – Michael De Silva Jun 20 '12 at 16:53
  • overriding that method fails for me too. I got a strange error **NoMethodError (undefined method `collect' for true:TrueClass):** – Lesha Pipiev Jun 20 '12 at 16:56