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