如何使用Sunspot在Rails中使用枚举? [英] How Do I Scope Enums in Rails Using Sunspot?

查看:123
本文介绍了如何使用Sunspot在Rails中使用枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Sunspot(Rails Solr gem)来使用我在模型中声明的枚举来对结果进行范围调整。我的模型的相关部分如下所示:

I am trying to use Sunspot (Rails Solr gem) to scope results using an enum I have declared in my model. The relevant portion of my model looks like this:

searchable do
  text :tag_list
  boolean :approved
  integer :perspective
  time :created_at
end

enum perspective: [ :not_applicable, :front, :side_front, :side, :side_back, :back, :above, :below ]


$ b

My search block in my controller looks like this:

def index
  //skip scoping if perspective is nil
  params[:perspective] ||= []

  @search = Upload.search do
    with :approved, true
    with :perspective, params[:perspective]
    fulltext params[:tag]
    fulltext params[:search] do
      minimum_match 1
    end
    paginate page: params[:page], per_page: 30
  end
    @uploads = @search.results
    @query = params[:search]
end

我正在使用的链接收集参数如下:

And the link I'm using to collect the params looks like this:

<%= link_to upload.perspective.humanize, search_index_path(perspective: Upload.perspectives[upload.perspective]), class: "perspective" %>

基本上我试图设置它,以便它返回所有这些标准在范围界定后通过批准。我已经使用基于文本的搜索条件,但透视图的范围不起作用。搜索只是返回0的所有结果,并且没有任何其他值。我是否缺少关于Sunspot / Solr如何与枚举一起使用的内容?当我做一个查询,如Upload.where(透视:1)我得到正确的结果,所以我不知道为什么这不工作。

Basically I'm trying to set this up so that it returns the results of any combination of all of these criteria after scoping by approved. I've got it working with the text based search criteria, but the scoping by perspective is not working. The search just returns all the results for 0 and none for any other value. Am I missing something about how Sunspot/Solr works with enums? When I do a query such as Upload.where(perspective: 1) I get the proper results, so I'm not sure why this isn't working.

谢谢提前。

推荐答案

我终于得到了一个解决。问题是尽管Rails将枚举值作为整数存储在数据库中,但是当您调用enum属性(在本例中为@ upload.perspective)时,它不会检索该整数。这样返回一个与该名称相对应的字符串(在这种情况下为front,side等))。这个问题是在我可搜索的块中,我将透视字段声明为整数。

I finally got this one solved. The issue was that although Rails stores the enum value as an integer in the database, it does not retrieve that integer when you call the enum attribute (in this case @upload.perspective). This returns a string corresponding to the name of the value instead ("front", "side", etc. in this case). The problem with this is that in my searchable block I was declaring the perspective field as an integer.

解决方案:

一个在模型中获取枚举id整数而不是名称的方法:

A method in the model which grabs the enum id integer rather than the name:

def perspective_id
  Upload.perspectives[self.perspective]
end

修改可搜索的块:

searchable do
  text :tag_list
  boolean :approved
  integer :perspective_id
  time :created_at
end

控制器中的搜索块:

def index
  #if no perspective, set to empty hash so solr skips
  params[:perspective] ||= []
  @search = Upload.search do
    with :approved, true
    with :perspective_id, params[:perspective]
    fulltext params[:tag]
    fulltext params[:search] do
      minimum_match 1
    end
    paginate page: params[:page], per_page: 30
  end
    @uploads = @search.results
    @query = params[:search]
end

这篇关于如何使用Sunspot在Rails中使用枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆