1

我正在尝试在我的 rails 应用程序中实现全文搜索。该应用程序基于Comfort_mexican_sofa。对于全文,我使用的是sunspot gem。我需要将可搜索属性添加到 Pages 类。我在具有相同类的模型下创建了一个新文件,但它似乎不起作用。我怎样才能扩展这个类?我收到错误

`undefined method `search' for #<Class:0x007fbe90f6faa8>`

应用程序/模型/Page.rb

class Comfy::Cms::Page < ActiveRecord::Base
  searchable do
    text :label, :content_cache
    boolean :featured
  end
end

文章控制器.rb

def index
    since = params[:sinceDate]
    query = params[:searchQuery]
    @articles = nil
    if query
      @articles = Comfy::Cms::Page.search{ fulltext query }.published.all
    else
      @articles = Comfy::Cms::Page.published.all
    end
    if since
      @articles = @articles.reject{ |a| a[:created_at] < Date.parse(since) }
    end
    # @articles
    # if query
    #   @articles = @articles.select{ |a| a[:label].match(/#{query}/i) }
    # end
  end
4

1 回答 1

2

您可以尝试这样做:

创建一个文件并将其放入您的初始化程序文件夹:

Comfy::Cms::Page.class_eval do
  searchable do
    text :label, :content_cache
    boolean :featured
  end
end
于 2015-02-06T04:17:28.067 回答