范围不识别两列?

时间:2011-12-10 16:37:49

标签: ruby-on-rails ruby ruby-on-rails-3 jquery-tokeninput

在我的令牌输入自动填充字段中,当我按照定义的:address方法进行操作时,我尝试将返回的列设为我的:website:store

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

我的json页面:http://localhost:3000/business_stores.json正确显示了所有结果,但令牌字段仅显示:address结果,而不显示网站结果。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

试试这个:

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