2

我有一个具有多个“has_many through”关系的 rails 模型。我在活动记录和可搜索块中定义了这些。当我在一个单词模型名称(例如:Contributor)上定义关系并重新索引时,它工作正常。当我在 2 字模型名称(例如:LuSubject)上定义关系并重新索引时,它不起作用。我收到错误“未定义的方法'subject_ids'。我尝试了其他具有相同结果的模型。“奖”有效。“LuGrade”没有。

这是我的“工作”模型中的代码:

class Work < ActiveRecord::Base

  self.table_name = 'work'
  self.primary_keys = :WORK_ID  

  alias_attribute :work_id, :WORK_ID
  alias_attribute :title, :TITLE
  alias_attribute :series_id, :SERIES_ID 


  has_many :work_contributors, :foreign_key => :WORK_ID
  has_many :contributors, :through => :work_contributors
  has_many :work_grades, :foreign_key => :WORK_ID
  has_many :lu_grades, :through => :work_grades  
  has_many :work_primary_subjects, :foreign_key => :WORK_ID
  has_many :lu_subjects, :through => :work_primary_subjects  
  has_many :work_awards, :foreign_key => :WORK_ID
  has_many :awards, :through => :work_awards
  belongs_to :series, :foreign_key => :SERIES_ID

  searchable do
    text :title 

    integer :work_id 
    string :title
    integer :series_id 
    integer :contributor_ids, :references => Contributor, :multiple => true
    integer :subject_ids, :references => LuSubject, :multiple => true
  end

这是重新索引命令的输出

$ bundle exec rake sunspot:reindex 
rake aborted!
undefined method `subject_ids' for #<Work:0x00000003bee760>

Tasks: TOP => sunspot:reindex
(See full trace by running task with --trace)

这是完整的跟踪

$ bundle exec rake sunspot:reindex --trace
** Invoke sunspot:reindex (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute sunspot:reindex
rake aborted!
undefined method `subject_ids' for #<Work:0x00000004001690>
/var/lib/gems/1.9.1/gems/activemodel-3.2.6/lib/active_model/attribute_methods.rb:407:in     `method_missing'
/var/lib/gems/1.9.1/gems/activerecord-3.2.6/lib/active_record/attribute_methods.rb:149:in `method_missing'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/data_extractor.rb:17:in `value_for'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/field_factory.rb:58:in  `populate_document'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/indexer.rb:95:in `block in prepare '
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/indexer.rb:94:in `each'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/indexer.rb:94:in `prepare'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/indexer.rb:24:in `block in add'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/indexer.rb:24:in `map'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/indexer.rb:24:in `add'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/session.rb:91:in `index'
/var/lib/gems/1.9.1/gems/sunspot- 1.3.3/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `index'
/var/lib/gems/1.9.1/gems/sunspot- 1.3.3/lib/sunspot/session_proxy/retry_5xx_session_proxy.rb:17:in `method_missing'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `index'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot.rb:184:in `index'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/searchable.rb:256:in  `block (2 levels) in solr_index'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/searchable.rb:345:in  `solr_benchmark'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/searchable.rb:255:in  `block in solr_index'
/var/lib/gems/1.9.1/gems/activerecord-3.2.6/lib/active_record/relation/batches.rb:72:in `find_in_batches'
/var/lib/gems/1.9.1/gems/activerecord-3.2.6/lib/active_record/querying.rb:8:in `find_in_batches'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/searchable.rb:254:in `solr_index'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/searchable.rb:198:in `solr_reindex'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/tasks.rb:58:in `block (3 levels) in <top (required)>'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/class_set.rb:16:in `each'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/class_set.rb:16:in `each'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/tasks.rb:57:in `block (2 levels) in <top (required)>'
/usr/lib/ruby/vendor_ruby/rake/task.rb:205:in `call'
/usr/lib/ruby/vendor_ruby/rake/task.rb:205:in `block in execute'
/usr/lib/ruby/vendor_ruby/rake/task.rb:200:in `each'
/usr/lib/ruby/vendor_ruby/rake/task.rb:200:in `execute'
/usr/lib/ruby/vendor_ruby/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/lib/ruby/vendor_ruby/rake/task.rb:151:in `invoke_with_call_chain'
/usr/lib/ruby/vendor_ruby/rake/task.rb:144:in `invoke'
/usr/lib/ruby/vendor_ruby/rake/application.rb:116:in `invoke_task'
/usr/lib/ruby/vendor_ruby/rake/application.rb:94:in `block (2 levels) in top_level'
/usr/lib/ruby/vendor_ruby/rake/application.rb:94:in `each'
/usr/lib/ruby/vendor_ruby/rake/application.rb:94:in `block in top_level'
/usr/lib/ruby/vendor_ruby/rake/application.rb:133:in `standard_exception_handling'
/usr/lib/ruby/vendor_ruby/rake/application.rb:88:in `top_level'
/usr/lib/ruby/vendor_ruby/rake/application.rb:66:in `block in run'
/usr/lib/ruby/vendor_ruby/rake/application.rb:133:in `standard_exception_handling'
/usr/lib/ruby/vendor_ruby/rake/application.rb:63:in `run'
/usr/bin/rake:27:in `<main>'
Tasks: TOP => sunspot:reindex

这是我当前的版本信息

$ gem list|grep sunspot
sunspot (1.3.3)
sunspot_rails (1.3.3)
sunspot_solr (1.3.3)

注意:我正在使用生产表,所以我没有重命名这些表的选项。

谢谢。

4

1 回答 1

1

您的设置将导致 Sunspot在您的对象上integer :subject_ids, ...调用方法。subject_ids一些 ActiveRecord 关联将基于关联定义类似的方法has_many,但我看不到has_many :subjects哪个会定义它。

你的意思是integer :lu_subject_ids

于 2012-08-29T17:22:21.317 回答