Search code examples
ruby-on-railsrubyruby-on-rails-3jquery-tokeninput

Scope is not recognizing both columns?


On my Tokeninput autocomplete field I am trying to make the returned columns be both my :address and :website when it goes by the defined :store method.

class BusinessStore < ActiveRecord::Base
    scope :search_by_store, lambda { |q|
       (q ? where(["address LIKE ? or website LIKE ? like ?", '%'+ q + '%', '%'+ q + '%','%'+ q + '%' ])  : {})}

    def store
        if self.online_store
          "#{business_name} - #{website}"
        else
          "#{business_name} - #{address}"
        end
    end
end

class BusinessStoresController < ApplicationController

  def index
    @business_stores = BusinessStore.all
    @business_stores = BusinessStore.search_by_store(params[:q])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @business_stores }
      format.json { render :json => @business_stores.collect{|b|{:id => b.id, :name => b.store } } }
    end
  end
end

My json page : http://localhost:3000/business_stores.json shows all the results correctly but the Token field only shows :address results and not website ones. How do I fix this?


Solution

  • Try this:

    (q ? where(["address LIKE ? OR website LIKE ?", "%#{q}%", "%#{q}%" ]) : {})}