【发布时间】:2016-01-18 21:17:00
【问题描述】:
我正在尝试使用 pg_search 来搜索两个模型。我有一个包含两列“child_id”和“book_id”的“Hires”模型。我想在 'Children' 模型中索引与 child_id 关联的子名称。
我的模型如下所示:
Hire.rb
class Hire < ActiveRecord::Base
belongs_to :book
belongs_to :child
accepts_nested_attributes_for :book
accepts_nested_attributes_for :child
include PgSearch
multisearchable :against => [:child_id, child_forename]
def child_forename
child.forename
end
end
但是当我尝试构建索引时 (rake pg_search:multisearch:rebuild[Hires]) 我收到以下错误:
rake aborted!
NameError: undefined local variable or method `child_forename' for Hire (call 'Hire.connection' to establish a connection):Class
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
/Users/James/Documents/websites/STAlibrary/app/models/hire.rb:12:in `<class:Hire>'
/Users/James/Documents/websites/STAlibrary/app/models/hire.rb:1:in `<top (required)>'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:457:in `load'
我如何构建这个查询来索引孩子的名字?
【问题讨论】:
标签: ruby-on-rails ruby postgresql ruby-on-rails-4 pg-search