【问题标题】:Rails 4 and gem sunspot cannot order correctlyRails 4 和 gem sunspot 无法正确排序
【发布时间】:2015-04-22 06:18:41
【问题描述】:

我收到此错误:

Sunspot::UnrecognizedFieldError in SitesController#products
No field configured for Product with name 'created_at'

这是我的模型Product.rb

class Product < ActiveRecord::Base
    searchable do 
      text :name
      text :description
      text :specification

      string  :name
    end
end

这是我的控制器中的一个方法:

def list_all_products
    @search = Product.search do
      fulltext params[:search]
      order_by :created_at, :desc
    end

    @products = @search.results
  end

我的表 products 中的 name 字段是一个 string。我在可搜索产品中定义了文本和字符串。为什么我仍然收到这样的错误?谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby sunspot-rails sunspot-solr


    【解决方案1】:

    只需在您的模型中添加time :created_at

     class Product < ActiveRecord::Base
       searchable do 
         text :name
         text :description
         text :specification
         time :created_at
    
         string  :name
       end
     end
    

    【讨论】:

    • 它现在不会引发错误,但产品没有按照 created_at 的 desc 进行相应的订购:(
    • 你用的是什么数据库?
    • 所以我想!我对 postgre 也有同样的问题,当有搜索时,范围不能正常工作!正在开发 sqlite3,一切正常
    • 我明白了,所以这是一个问题,我不能在 postgresql 中使用 sunspot :(
    • 如果你想在postgresql中使用搜索,这个问题总是会出现,范围结果会不正确
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 2015-07-02
    相关资源
    最近更新 更多