Pergunta

I have a rails model that has multiple "has_many through" relationships. I defined these in active record and in the searchable block. When I define the relationship on a single-word model name (ex: Contributor) and re-index, it works fine. When I define the relationship on a 2-word model name (ex: LuSubject) and re-index, it does not work. I get the error "undefined method 'subject_ids'. I tried other models with the same result. "Award" works. "LuGrade" does not.

Here is the code from my "Work" model:

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

Here is the output of the reindex command

$ 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)

Here is the full 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

Here is my current version info

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

NOTE: I am working with production tables, so I don't have the option of renaming those tables.

Thanks.

Foi útil?

Solução

Your setting of integer :subject_ids, ... will cause Sunspot to invoke a subject_ids method on your object. Some ActiveRecord associations will define a method like that based on a has_many association, but I don't see a has_many :subjects which would define that.

Did you mean integer :lu_subject_ids?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top