【问题标题】:How to use pg_search with globalize?如何将 pg_search 与 globalize 一起使用?
【发布时间】:2015-03-13 15:14:21
【问题描述】:

我使用 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 吗?

【问题讨论】:

  • is not working - 什么意思???
  • search_everywhere 找不到任何字段。

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


【解决方案1】:

我找到了正确的解决方案: 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多