0

我使用pg_search gem在我的rails應用程序中進行搜索。在我的應用程序的主要模式是:如何使用pg_search進行全球化?

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

它在BooksController中的索引行動工作完美與

@books = Book.search_everywhere(params[:search]) 

。然後我添加寶石「全球化」:

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

而現在搜索不起作用。

Book.search_everywhere(params[:search]) 

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

+0

'不工作' - 這是什麼意思? – 2015-03-13 15:17:41

+0

search_everywhere找不到任何字段。 – Victor 2015-03-13 19:15:11

回答

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