如何将pg_search与globalize结合使用?

问题描述 投票:0回答:2

我使用 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 'globalize':

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

现在搜索不起作用。

Book.search_everywhere(params[:search])

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

ruby-on-rails search internationalization pg-search globalize
2个回答
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

0
投票

您不需要创建 BookTranslation 模型。这是更简单的解决方案:

class Book < ActiveRecord::Base
  include PgSearch
  translates :title, :author, :publisher, :description, :fallbacks_for_empty_translations => true
  pg_search_scope :search_everywhere, :associated_against  => {:translations => [:title, :author, :description]}
end
© www.soinside.com 2019 - 2024. All rights reserved.