【问题标题】:Scope is not recognizing both columns?范围不识别两个列?
【发布时间】:2011-12-10 16:37:48
【问题描述】:

在我的 Tokeninput 自动完成字段中,我试图通过定义的 :store 方法使返回的列同时成为我的 :address:website

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 正确显示所有结果,但 Token 字段仅显示 :address 结果而不是网站结果。我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 谢谢先生!欣赏它。
    猜你喜欢
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多