如何更正 HABTM 关系中的 'SystemStackError (stack level too deep)'?

How can I correct a 'SystemStackError (stack level too deep)' on HABTM relationship?

我正在尝试 debug/resolve 我在尝试添加到我的报表模型上的关联时遇到的错误。我不断收到 SystemStackError (stack level too deep) 错误,我读过这可能是由于无限循环调用引起的——但我似乎无法找到它。这是我的模型和控制器。我已将范围缩小到尝试将 "Alerts" 添加到 "Report" 时。如果我需要包含更多内容,请告诉我。谢谢!

举报

class Report < ActiveRecord::Base
  self.inheritance_column = :disabled

  # Associations

  has_and_belongs_to_many :alerts
  accepts_nested_attributes_for :alerts, allow_destroy: true, reject_if: :all_blank

  validates_uniqueness_of :author_id, scope: :player_id

  public

    def all_alerts
      alerts_string = ""
      alerts.order(letter: :asc).each do |a|
        alerts_string += a.letter.upcase
      end
      alerts_string
    end

end

警报

class Alert < ActiveRecord::Base
  self.inheritance_column = :disabled

  has_and_belongs_to_many :reports

  public

    def to_s
      "#{letter.upcase}"
    end

end

玩家::ReportsController

class Players::ReportsController < ApplicationController

  # ... other actions

  def update
    respond_to do |format|
      if @report.update(report_params)
        format.html { redirect_to player_report_path(@report.player, @report), notice: 'Report was successfully updated.' }
        format.json { render :show, status: :ok, location: player_report_path(@report.player, @report) }
      else
        format.html { render :edit }
        format.json { render json: @report.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @report.destroy
    respond_to do |format|
      format.html { redirect_to player_reports_path(@report.player), notice: 'Report was successfully destroyed.' }

      format.json { head :no_content }
    end
  end

  private

  # Never trust parameters from the scary internet, only allow the white list through.
    def report_params
      params.require(:report).permit(:author_id, :player_id, { alert_ids:[] })
    end
end

编辑:添加堆栈跟踪

    bullet (4.14.9) lib/bullet/active_record42.rb:194:in `has_cached_counter?'
    bullet (4.14.9) lib/bullet/active_record42.rb:194:in `has_cached_counter?'
    bullet (4.14.9) lib/bullet/active_record42.rb:194:in `has_cached_counter?'
    bullet (4.14.9) lib/bullet/active_record42.rb:194:in `has_cached_counter?'
    bullet (4.14.9) lib/bullet/active_record42.rb:194:in `has_cached_counter?'
    bullet (4.14.9) lib/bullet/active_record42.rb:205:in `has_cached_counter?'
    activerecord (4.2.0) lib/active_record/associations/has_many_through_association.rb:67:in `insert_record'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:559:in `block (2 levels) in concat_records'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:398:in `replace_on_target'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:393:in `add_to_target'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:558:in `block in concat_records'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:556:in `each'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:556:in `concat_records'
    activerecord (4.2.0) lib/active_record/associations/has_many_association.rb:165:in `concat_records'
    activerecord (4.2.0) lib/active_record/associations/has_many_through_association.rb:44:in `concat_records'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:158:in `block in concat'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:173:in `block in transaction'
    activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
    activerecord (4.2.0) lib/active_record/transactions.rb:220:in `transaction'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:172:in `transaction'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:158:in `concat'
    activerecord (4.2.0) lib/active_record/associations/has_many_through_association.rb:38:in `concat'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:536:in `replace_records'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:363:in `block in replace'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:173:in `block in transaction'
    activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
    activerecord (4.2.0) lib/active_record/transactions.rb:220:in `transaction'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:172:in `transaction'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:363:in `replace'
    activerecord (4.2.0) lib/active_record/associations/collection_association.rb:67:in `ids_writer'
    activerecord (4.2.0) lib/active_record/associations/builder/collection_association.rb:76:in `alert_ids='
    activerecord (4.2.0) lib/active_record/attribute_assignment.rb:54:in `public_send'
    activerecord (4.2.0) lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
    activerecord (4.2.0) lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
    actionpack (4.2.0) lib/action_controller/metal/strong_parameters.rb:183:in `each_pair'
    actionpack (4.2.0) lib/action_controller/metal/strong_parameters.rb:183:in `each_pair'
    activerecord (4.2.0) lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
    activerecord (4.2.0) lib/active_record/persistence.rb:249:in `block in update'
    activerecord (4.2.0) lib/active_record/transactions.rb:347:in `block in with_transaction_returning_status'
    activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
    activerecord (4.2.0) lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
    activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
    activerecord (4.2.0) lib/active_record/transactions.rb:220:in `transaction'
    activerecord (4.2.0) lib/active_record/transactions.rb:344:in `with_transaction_returning_status'
    activerecord (4.2.0) lib/active_record/persistence.rb:248:in `update'
    app/controllers/players/reports_controller.rb:67:in `block in update'
actionpack (4.2.0) lib/action_controller/metal/mime_responds.rb:211:in `respond_to'
app/controllers/players/reports_controller.rb:66:in `update'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:308:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:308:in `block (2 levels) in halting'
rails-observers (0.1.2) lib/rails/observers/action_controller/caching/sweeping.rb:73:in `around'
activesupport (4.2.0) lib/active_support/callbacks.rb:450:in `public_send'
activesupport (4.2.0) lib/active_support/callbacks.rb:450:in `block in make_lambda'
activesupport (4.2.0) lib/active_support/callbacks.rb:307:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:307:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
bullet (4.14.9) lib/bullet/rack.rb:12:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:31:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/home/daveomcd/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/home/daveomcd/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/home/daveomcd/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

顶行 bullet (4.14.9) lib/bullet/active_record42.rb:194:inhas_cached_counter?'` 出现了 50 多次。

对我来说这似乎是一个可能的 Bullet 问题,也许您应该 post 将其作为他们 Github 的一个问题? Bullet 在它的 lib/acive_record42.rb 文件中有一段代码:

alias_method :origin_has_cached_counter?, :has_cached_counter?
def has_cached_counter?(reflection = reflection())
  result = origin_has_cached_counter?(reflection)
  ...
end

看起来像是一个可能产生 SLTD 的地方,但很有可能我错了。