如何在全局化中使用pg_search?

时间:2015-03-13 15:14:21

标签: ruby-on-rails search internationalization pg-search globalize

我使用pg_search gem在我的rails应用程序中搜索。我的应用程序中的主要模型是:

class Book < ActiveRecord::Base
  include PgSearch
  pg_search_scope :search_everywhere, against: [:author, :title, :description]
end

完美配合
@books = Book.search_everywhere(params[:search])
在BooksController的索引操作中。然后我添加了gem&#39; globalize&#39;:

  translates :title, :author, :publisher, :description, :fallbacks_for_empty_translations => true

现在搜索无效。

Book.search_everywhere(params[:search])

无法找到任何字段。 有人用pg_search gem和globalize gem吗?

1 个答案:

答案 0 :(得分:0)

我找到了正确的解决方案: Gem'globalize'创建了新表“book_translations”。所以我应该在这个表中搜索:

#book.rb
class Book < ActiveRecord::Base
  include PgSearch
  has_many :book_translations
  translates :title, :author, :publisher, :description, :fallbacks_for_empty_translations => true
  pg_search_scope :search_everywhere, :associated_against  => {:book_translations => [:title, :author, :description]}
end

#book_translation.rb
class BookTranslation < ActiveRecord::Base
  belongs_to :book
end