我使用rails 5.1.4Sphinx 2.2.11-id64-release (95ae9a6)
预期行为:
当我提交新的搜索时,我希望看到一个空数组thinking_sphinx v 4.0.0或一组搜索结果。
实际行为:
当我从视图层提交一个带有空参数的新搜索,并试图通过控制器操作中的[]访问thinkingsphinx::search对象时,rails抛出一个binding.pry

[1] pry(main)> ThinkingSphinx.search ''
=> [#<ThinkingSphinx::Search:0x2b0925399e10>
[2] pry(main)> _.inspect
RuntimeError: Circular dependency detected while autoloading constant StudentLesson
from /home/kf/.rvm/gems/ruby-2.4.3@crm/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:509:in `load_missing_constant'
[3] pry(main)>

代码段:
class Lesson < ApplicationRecord
  LESSON_TYPES = {
    'StudentLesson': StudentLesson,
    'ProfessionalLesson': ProfessionalLesson
  }.freeze
end

class StudentLesson < Lesson
  after_save ThinkingSphinx::RealTime.callback_for(:student_lesson)
end

class ProfessionalLesson < Lesson
  after_save ThinkingSphinx::RealTime.callback_for(:professional_lesson)
end

# app/indices/student_lesson_index.rb
ThinkingSphinx::Index.define :student_lesson, with: :real_time do
  indexes name, sortable: true
end

# app/indices/professional_lesson_index.rb
ThinkingSphinx::Index.define :professional_lesson, with: :real_time do
  indexes name, sortable: true
end

class SearchesController < ApplicationController
  def index
    @results = []
  end

  def create
    @results = ThinkingSphinx.search(params[:search])
    render :index
  end
end

<div class="collapse navbar-collapse" id="header-navbar">
   <%= render 'layouts/nav_links' %>
   <%= form_for searches_path do %>
     <%= search_field_tag :search, params[:search] %>
     <%= submit_tag 'Search', name: nil, method: :get %>
   <% end %>
 </div>

这是ActionView::Template::Error (Circular dependency detected while autoloading constant StudentLesson)
indexer
{
}

searchd
{
  listen = 127.0.0.1:9306:mysql41
  log = /home/myapp/log/development.searchd.log
  query_log = /home/myapp/log/development.searchd.query.log
  pid_file = /home/myapp/log/development.sphinx.pid
  workers = threads
  binlog_path = /home/myapp/tmp/binlog/development
}

index game_core
{
  type = rt
  path = /home/myapp/db/sphinx/development/game_core
  docinfo = extern
  rt_field = sphinx_internal_class_name
  rt_field = name
  rt_field = summary
  rt_attr_uint = sphinx_deleted
  rt_attr_bigint = sphinx_internal_id
  rt_attr_timestamp = created_at
  rt_attr_timestamp = updated_at
  rt_attr_string = sphinx_internal_class
  rt_attr_string = name_sort
}

index lesson_core
{
  type = rt
  path = /home/myapp/db/sphinx/development/lesson_core
  docinfo = extern
  rt_field = sphinx_internal_class_name
  rt_field = name
  rt_field = purpose
  rt_field = meta
  rt_field = supplies
  rt_field = activity
  rt_attr_uint = sphinx_deleted
  rt_attr_bigint = sphinx_internal_id
  rt_attr_timestamp = created_at
  rt_attr_timestamp = updated_at
  rt_attr_string = sphinx_internal_class
  rt_attr_string = name_sort
}

index protocol_core
{
  type = rt
  path = /home/myapp/db/sphinx/development/protocol_core
  docinfo = extern
  rt_field = sphinx_internal_class_name
  rt_field = name
  rt_field = description
  rt_attr_uint = sphinx_deleted
  rt_attr_bigint = sphinx_internal_id
  rt_attr_timestamp = created_at
  rt_attr_timestamp = updated_at
  rt_attr_string = sphinx_internal_class
  rt_attr_string = name_sort
}

index resource_page_core
{
  type = rt
  path = /home/myapp/db/sphinx/development/resource_page_core
  docinfo = extern
  rt_field = sphinx_internal_class_name
  rt_field = header
  rt_field = content
  rt_attr_uint = sphinx_deleted
  rt_attr_bigint = sphinx_internal_id
  rt_attr_timestamp = created_at
  rt_attr_timestamp = updated_at
  rt_attr_string = sphinx_internal_class
  rt_attr_string = header_sort
}

index game
{
  type = distributed
  local = game_core
}

index lesson
{
  type = distributed
  local = lesson_core
}

index protocol
{
  type = distributed
  local = protocol_core
}

index resource_page
{
  type = distributed
  local = resource_page_core
}

最佳答案

我认为这里的问题与思考sphinx没有直接关系-它只是错误,因为它无法加载搜索结果,因为您的模型中存在循环依赖关系-特别是LESSON_TYPES常量:
Thinking Sphinx进行搜索调用,并且在其结果集中至少有一个StudentLesson实例,因此它需要加载该模型。
LoadingStudentLessonLesson上找到它的依赖项(作为子类)。
LoadingLesson找到它对StudentLessonProfessionalLesson的依赖关系(作为常量的引用)。
因此,StudentLesson试图再次加载,因此依赖关系的循环是无止境的。
(FWIW我刚刚使用您提供的模型代码在一个test Rails应用程序中确认了这种行为,没有涉及到TS:我在控制台中运行所需的全部是StudentLesson.first

关于ruby-on-rails - 如何在无循环依赖的情况下使用Think Sphinx在Rails 5中索引多态(STI)模型?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50084800/

10-16 22:40