使用STI时,使用Thinking Sphinx范围获取“未定义的方法”

时间:2012-11-13 10:17:35

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

我正在使用Thinking Sphinx 2.0.13和Rails 3.2.9。

鉴于我有和STI类看起来像这样:

class User < ActiveRecord::Base
  define_index do
    has :account_id
    has :is_deleted
  end

  sphinx_scope(:by_account) do |account_id|
    {:with => {:account_id => account_id}}
  end

  sphinx_scope(:without_deleted) do
    {:with => {:is_deleted => false}}
  end
end


class Admin < User
end

如果我尝试在User或Admin类上使用单个作用域,则一切正常。我也可以按照预期使用User模型将范围链接在一起。问题是,如果我在Admin模型上链接范围,我得到:

> Admin.by_account(1).without_deleted

NoMethodError:   Sphinx Query (2.9ms)  
  Sphinx  Found 3 results
  Admin Load (0.6ms)  SELECT `users`.* FROM `users` WHERE `users`.`type` IN ('Admin') AND `users`.`id` IN (7, 8, 9)
undefined method `without_deleted' for #<ThinkingSphinx::Search:0x007fd3d95f7a08>

遇到第一个范围时,它似乎正在运行查询。有没有明显的东西我缺少,或者这看起来像TS的问题?

1 个答案:

答案 0 :(得分:0)

以下是人们使用sphinx_scopes之前可能遇到的问题。

  • 人们在sphinx_scopes中发现了名称冲突问题。您的范围by_account是其中的主要候选者,因此请尝试重命名。我可以想象在派生类而不是基类中搞乱的情况。
  • 过去的范围问题已通过重新排序来解决,因此请尝试Admin.without_deleted.by_account(1)。我知道,不是解决办法。
  • 我还预测account_id是主键(即每User最多一个Account)。如果是这样,这可以解释为什么Rails选择提前获取它。
相关问题