6

我想在标准搜索之外使用方面。有没有办法使用 pg_search 使搜索结果本身通过构面“搜索”?

据我所知, pg_search_scope 是互斥的(有解决方法吗?)。谢谢!

例子:

1) 搜索带有单词“test”的博客

2) 单击链接仅获取先前结果中同样在 6 月发布的文章

4

1 回答 1

8

我是pg_search.

Apg_search_scope与任何其他 Active Record 范围一样工作,因此您可以将它们链接起来。

因此,假设您有一个模型,该模型Blog具有一个pg_search_scope命名search_title范围和另一个命名范围in_month,它接受两个参数,一个月份编号和一个年份编号。像这样的东西:

class Blog < ActiveRecord::Base
  include PgSearch
  pg_search_scope :search_title, :against => :title
  scope :in_month, lambda { |month_number, year_number| 
    where(:month => month_number, :year => year_number)
  }
end

然后你可以这样称呼它:

Blog.search_title("broccoli").in_month(6, 2011)

反过来也应该起作用:

Blog.in_month(6, 2011).search_title("broccoli")

最后也可以调用像 Kaminari 这样的分页解决方案。

于 2011-10-22T16:01:00.563 回答