在comfortable_mexican_matress中扩展模型

时间:2015-02-05 23:12:46

标签: ruby-on-rails ruby full-text-search sunspot-rails comfortable-mexican-sofa

我试图在我的rails应用程序中实现全文搜索。该应用程序基于comfortable_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

articles_controller.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

1 个答案:

答案 0 :(得分:2)

您可以尝试这样做:

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

Comfy::Cms::Page.class_eval do
  searchable do
    text :label, :content_cache
    boolean :featured
  end
end