Suavocado
Suavocado

Reputation: 969

Rails-JQuery-Autocomplete search multiple attributes

I'm using the rails-jquery-autocomplete gem to find a customer in a form.
Controller

class RfqsController < ApplicationController
  autocomplete :customer, :name, full:true

view file

  <div class="field">
    <%= f.label :customer %>
    <%=  f.autocomplete_field :customer, :autocomplete_customer_name %>
    <%= link_to "new customer", new_customer_path, class: "new_customer_link"%>
  </div>

Currently, It only searches by the customer name. I would like to have it search by the company as well. I'm new to rails but I think I need to use scopes but I'm not sure how to go about it. Any help is appreciated.

Upvotes: 0

Views: 538

Answers (1)

Maged Makled
Maged Makled

Reputation: 1986

Extra_data

From the link you have provided https://github.com/bigtunacan/rails-jquery-autocomplete By default, your search will only return the required columns from the database needed to populate your form, namely id and the column you are searching (name, in the above example).

Passing an array of attributes/column names to this option will fetch and return the specified data.

class ProductsController < Admin::BaseController
  autocomplete :brand, :name, :extra_data => [:slogan]
end

so in your case


autocomplete :customer, :name, full:true, :extra_data => [:company_name]

def company_name
 company.name
end

Upvotes: 1

Related Questions