我有一个有多个“has_many through”关系的rails模型。 我在活动记录和可搜索块中定义了这些。 当我在单字模型名称上定义关系时 (例如:贡献者)和重新索引,它工作正常。 当我在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
这是reindex命令的输出
$ 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)
注意:我正在使用生产表,因此我没有重命名这些表的选项。
感谢。
答案 0 :(得分:1)
您integer :subject_ids, ...
的设置将导致太阳黑子在您的对象上调用subject_ids
方法。一些ActiveRecord关联将基于has_many
关联定义类似的方法,但我没有看到定义该关联的has_many :subjects
。
您的意思是integer :lu_subject_ids
吗?