3

有没有办法使用 Sunspot 和 Rails 索引整数列表?

例子

我希望能够搜索演员 ID。
下面的代码只是伪代码。

class Movie < ActiveRecord::Base
  searchable do
    integers :actors_lists do
      actors.map(&:id)
    end
  end
end

不需要搜索演员姓名的能力,我只需要能够搜索演员 ID。

我正在使用带有 Sunspot 1.2.1 和 Ruby 1.9.2 的 Rails 3.1 RC。

4

2 回答 2

3

这是我的解决方案。

integer :actors_lists, multiple: true do
  actors.map(&:id)
end

添加multiple: true零件解决了这个问题。

注意:如果您使用的是 Ruby 1.8.7,请记住更改multiple: true:multiple => true.

于 2011-07-06T07:48:12.853 回答
0

如果电影 has_and_belongs_to_many :actors 那么

integer :actor_ids, multiple => true

应该管用。搜索参数的名称与您想要的不同,但这是一种“约定优于配置”的情况,在这里您可以使用

:references => ::Actor

只是想我应该提到所有选项。

于 2011-09-21T08:15:44.573 回答