思考Sphinx与has_many通过索引就好但保存项目崩溃

时间:2012-06-21 16:11:37

标签: ruby-on-rails-3 thinking-sphinx

思考sphinx索引很好,查找在我的属性上运行良好。但是,我保存模型实例的尝试崩溃了。

class Location < ActiveRecord::Base
  has_many :operation_intervals_locations
  has_many :operation_intervals, :through => :operation_intervals_locations

  define_index "location" do
    # indexes here...

    # tried this syntax
    has operation_intervals(:start_int), :type => :integer 
    has operation_intervals(:end_int), :type => :integer
    has operation_intervals(:days_int), :type => :integer

    # and this one
    has operation_intervals.start_int, :type => :integer 
    has operation_intervals.end_int, :type => :integer
    has operation_intervals.days_int, :type => :integer
  end
end


class OperationInterval < ActiveRecord::Base
 attr_accessible :start_int, :end_int, :days_int  
end

每当我执行以下操作时:

Location.search("foo") # get the search initialized
l = Location.first
l.name = "bar"
l.save(:validate => false)

我得到以下内容:

# joining is working just fine
OperationInterval Load (0.3ms)  SELECT `operation_intervals`.* FROM `operation_intervals` INNER JOIN `operation_intervals_locations` ON `operation_intervals`.`id` = `operation_intervals_locations`.`operation_interval_id` WHERE `operation_intervals_locations`.`location_id` = 1

# here's where I'm getting my crash
NoMethodError: undefined method `end_int' for #<ActiveRecord::Relation:0xe048450>
from /usr/local/rvm/gems/ruby-1.9.3-p194@rails326/gems/activerecord-3.2.6/lib/active_record/relation/delegation.rb:45:in `method_missing'

编辑: 我正在使用延迟三角洲 - 延迟工作。我认为问题正在发生,因为thinking_sphinx试图将作业推向队列。

1 个答案:

答案 0 :(得分:0)

好的..思考sphinx适用于多对多关系

该行

has operation_intervals.days_int, :type => :integer

NOT 应该:type => :integer,因为一个位置可以有多个operation_intervals,而真正的类型就是数组!

使用此代码代替将解决问题:

has operation_intervals.days_int