【问题标题】:Rails3 jquery autocomplete distinct valuesRails3 jquery自动完成不同的值
【发布时间】:2010-12-18 16:54:19
【问题描述】:

是否可以使用 Rails3 JQuery 自动完成 gem 选择 DISTINCT 值?

【问题讨论】:

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


    【解决方案1】:

    使用范围更好更简单。

    在您的模型中:

    scope :uniquely_named, group(:name)
    

    在您的控制器中:

    autocomplete :user, :name, :scopes => [:uniquely_named]
    

    此解决方案可以轻松应用更复杂的过滤:

    型号:

    scope :uniquely_named, group(:name)
    scope :online, where(:online => true)
    

    控制器:

    autocomplete :user, :name, :scopes => [:uniquely_named, :online]
    

    source

    【讨论】:

      【解决方案2】:

      似乎没有 - 但是 -“自动完成”宏实际上只是在您的控制器中定义一个动作。我会放弃在我的控制器中使用“自动完成”,而是自己实现操作......

      def autocomplete_for_user_name
         User.select('distinct name')
      end
      

      祝你好运。

      【讨论】:

        【解决方案3】:

        我试试这个:

        class UbicationsController < ApplicationController
          autocomplete :ubication, :name
        
          def autocomplete_ubication_name
            render json: Ubication.select("DISTINCT name as value").where("LOWER(name) like LOWER(?)", "%#{params[:term]}%")
          end
        end
        

        【讨论】:

          【解决方案4】:

          其他答案均不适用于 MongoID。

          我混合了许多答案来做到这一点:

          class ProductsController < ApplicationController
            autocomplete :product, :manufacturer
            autocomplete :product, :model
          
            def autocomplete_product_manufacturer
              render_autocomplete(:manufacturer)
            end
          
            def autocomplete_product_model
              render_autocomplete(:model)
            end
          
            def render_autocomplete(field)
              render json: Product.where(field => /#{params[:term]}/i).where(domain: @domain).distinct(field)
            end
          end
          

          这不使用关系。制造商和型号是产品型号上的字符串字段。

          谢谢大家的回答。

          【讨论】:

            【解决方案5】:

            Postgresql:

               scope :uniquely_named, :select => 'distinct name' #In model
               autocomplete :user, :name, :scopes => [:uniquely_named] #In controller
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-07-27
              • 1970-01-01
              • 2011-12-17
              • 1970-01-01
              • 2011-06-19
              • 2012-05-15
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多