如何指定`associated_against`一个在pg_search中具有`as:`名称的关联?

时间:2015-07-24 23:16:59

标签: ruby-on-rails ruby-on-rails-4 pg-search

好的,所以我有一个Node模型,它具有以下关联:

class Node < ActiveRecord::Base
  belongs_to :family_tree
  belongs_to :user
  belongs_to :media, polymorphic: true, dependent: :destroy
  has_many :comments, dependent: :destroy
end

关键关联是media,它实际上是与Video的关联。这就是我的Video模型的外观:

class Video < ActiveRecord::Base
  has_one :node, as: :media
end

但我无法弄明白如何在pg_search_scope中指明这一点。

我试过这个:

  include PgSearch
  pg_search_scope :node_search, against: [:name, :user_id, :circa, :cached_user_tag_list, :cached_tagged_user_names],
    using: { tsearch: { any_word: true, dictionary: :english, prefix: true} },
    :associated_against => {
      video: [:description, :title]
    }

但我得到的错误是:

Completed 500 Internal Server Error in 131ms (ActiveRecord: 41.6ms)    
NoMethodError - undefined method `table_name' for nil:NilClass:

所以当我尝试这种方式时:

  include PgSearch
  pg_search_scope :node_search, against: [:name, :user_id, :circa, :cached_user_tag_list, :cached_tagged_user_names],
    using: { tsearch: { any_word: true, dictionary: :english, prefix: true} },
    :associated_against => {
      media: [:description, :title]
    }

我收到此错误:

Completed 500 Internal Server Error in 126ms (ActiveRecord: 17.7ms)    
NameError - uninitialized constant Node::Media:

如何指定该关联,或者这是pg_search不知道如何管理的边缘文件?

1 个答案:

答案 0 :(得分:2)

pg_search的维护者已经提到无法通过SQL直接搜索多态关联,因此不能用于pg_search - https://stackoverflow.com/a/15455017