0

我有一个具有 2 个属性的模型:

:image_filename 
:yt_video_id

我的控制器中有这段代码:

def index
   @search = Model.solr_search do |s|
   s.fulltext params[:search]
   s.paginate :page => params[:page], :per_page => 2
   s.with(:image_filename || :yt_video_id)
   end
   @model = @search.results
   respond_to do |format|
    format.html # index.html.erb
  end
 end

在我的model.rb模型中,我有这个searchable

searchable do
    string :image_filename, :yt_video_id
  end

我想要过滤器:image_filename :yt_video_id任何不是"nil"。我的意思是,这两个属性都必须具有强制值。

但我得到了错误:

Sunspot::UnrecognizedFieldError in ModelsController#index

No field configured for Model with name 'image_filename'
4

1 回答 1

2

通过以下步骤解决了该问题:

(这个解决方案对我来说很好。我希望这个解决方案也可以帮助你。)

model.rb你不能写这个语法:

searchable do
    string :image_filename, :yt_video_id
  end

您必须编写以下语法:

searchable do
      string :image_filename
      string :yt_video_id
     end

在 index 操作中的models_controller.rb中:

def index
   @search = Model.solr_search do |s|
   s.fulltext params[:search]
   s.paginate :page => params[:page], :per_page => 2
   s.any_of do
      without(:image_filename, nil)
      without(:yt_video_id, nil)
     end
   end
   @model = @search.results
   respond_to do |format|
    format.html # index.html.erb
   end
 end

我已经使用了该any_of方法。

要使用 OR 语义组合范围,请使用 any_of 方法将限制分组为析取:

Sunspot.search(Post) do
  any_of do
    with(:expired_at).greater_than(Time.now)
    with(:expired_at, nil)
  end
end

您可以在https://github.com/sunspot/sunspot/wiki/Scoping-by-attribute-fields中看到

于 2012-06-09T18:15:19.117 回答