title: Rails 5.1.6 - Stack level too deep
body: <p>I'm using Rails 5.1.6 with Puma and MySQL.
  I have the below code in a Rails controller:</p>
  
  <pre><code>class ScansController &lt; ApplicationController
    before_action :find_device, only: %i[index new create]
    .
    .
    .
    def create
      @scan = @device.scans.create(scan_params)
      flash[:notice] = t('.success') if @scan.save
      respond_with @scan.device, @scan
    end
    .
    .
    .
    private
  
    def find_device
      @device = Device.find params[:device_id]
    end
  
    def scan_params
      params.require(:scan).permit(:photo, :device_id, :reading, :reading_date)
    end
  end
  </code></pre>
  
  <p>And when I send a POST request to this URL the below happens:</p>
  
  <pre><code>Started POST "/en/devices/1/scans" for 81.0.89.20 at 2018-04-03 09:39:19 +0000
  09:39:19 web.1    | Processing by ScansController#create as HTML
  09:39:19 web.1    |   Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"QFdUs2thGPzJMbcWfApbattFMvnIEfzN1swrVGnbOTx7XbjY0Y3A8PLI1bRxqyBv6xIk9D2t13bfLPAqdSIyzQ==", "scan"=&gt;{"reading_date"=&gt;"2018-04-03T11:39:11", "reading"=&gt;"6"}, "button"=&gt;"", "locale"=&gt;"en", "device_id"=&gt;"1"}
  09:39:19 web.1    |   User Load (1.0ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  09:39:19 web.1    |   Device Load (0.6ms)  SELECT  `devices`.* FROM `devices` WHERE `devices`.`id` = 1 LIMIT 1
  09:39:19 web.1    |    (0.6ms)  BEGIN
  09:39:19 web.1    |    (0.7ms)  ROLLBACK
  09:39:19 web.1    | Completed 500 Internal Server Error in 33ms (ActiveRecord: 2.9ms)
  09:39:19 web.1    | 
  09:39:19 web.1    | 
  09:39:19 web.1    |   
  09:39:19 web.1    | SystemStackError (stack level too deep):
  09:39:19 web.1    |   
  09:39:19 web.1    | app/controllers/scans_controller.rb:35:in `create'
  </code></pre>
  
  <p>Now, line 35 is this line:<code>@scan = @device.scans.create(scan_params)</code></p>
  
  <p>and this was working well in Rails 4, but now that I migrated to Rails 5.1.6 it produces this behaviior even in my integration tests.
  I hav no clue what could be the reason for this behavior. Does anyone sees something my eyes avoided for the last one or two weeks?
  Thanx in advance.</p>
  
  <p>The models:</p>
  
  <pre><code>class ApplicationRecord &lt; ActiveRecord::Base
    self.abstract_class = true
  end
  
  class Scan &lt; ApplicationRecord
    .
    .
    belongs_to :device, inverse_of: :scans
    validates_attachment(
      :photo,
      content_type: {
        content_type: [
          'image/jpeg',
          'image/gif',
          'image/png',
          'image/tiff'
        ]
      }
    )
    with_options unless: @photo.present? do
      validates :reading, presence: true
      validates :reading, numericality: true
      validates :reading_date, presence: true
    end
    .
  end
  
  class Device &lt; ApplicationRecord
    .
    .
    has_many :scans, inverse_of: :device, dependent: :restrict_with_error
    validates :unit, presence: true
    validates :name, presence: true
    validates :serial, presence: true, uniqueness: true
    validates :owner, presence: true
    validates :precision, numericality: {
      only_integer: true,
      greater_than_or_equal_to: 0
    }, allow_nil: true
    . 
    .
  end
  </code></pre>
  
  <p>The Full stack trace looks like this:</p>
  
  <pre><code>activesupport (5.1.6) lib/active_support/callbacks.rb:827:in `_run_validate_callbacks'
  activemodel (5.1.6) lib/active_model/validations.rb:405:in `run_validations!'
  activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in `block in run_validations!'
  activesupport (5.1.6) lib/active_support/callbacks.rb:131:in `run_callbacks'
  activesupport (5.1.6) lib/active_support/callbacks.rb:827:in `_run_validation_callbacks'
  activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in `run_validations!'
  activemodel (5.1.6) lib/active_model/validations.rb:335:in `valid?'
  activerecord (5.1.6) lib/active_record/validations.rb:65:in `valid?'
  activesupport (5.1.6) lib/active_support/callbacks.rb:422:in `block in inverted_lambda'
  activesupport (5.1.6) lib/active_support/callbacks.rb:177:in `block (2 levels) in halting_and_conditional'
  activesupport (5.1.6) lib/active_support/callbacks.rb:177:in `each'
  activesupport (5.1.6) lib/active_support/callbacks.rb:177:in `all?'
  activesupport (5.1.6) lib/active_support/callbacks.rb:177:in `block in halting_and_conditional'
  activesupport (5.1.6) lib/active_support/callbacks.rb:507:in `block in invoke_before'
  activesupport (5.1.6) lib/active_support/callbacks.rb:507:in `each'
  activesupport (5.1.6) lib/active_support/callbacks.rb:507:in `invoke_before'
  activesupport (5.1.6) lib/active_support/callbacks.rb:130:in `run_callbacks'
  activesupport (5.1.6) lib/active_support/callbacks.rb:827:in `_run_validate_callbacks'
  activemodel (5.1.6) lib/active_model/validations.rb:405:in `run_validations!'
  activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in `block in run_validations!'
  activesupport (5.1.6) lib/active_support/callbacks.rb:131:in `run_callbacks'
  activesupport (5.1.6) lib/active_support/callbacks.rb:827:in `_run_validation_callbacks'
  activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in `run_validations!'
  activemodel (5.1.6) lib/active_model/validations.rb:335:in `valid?'
  activerecord (5.1.6) lib/active_record/validations.rb:65:in `valid?'
  activerecord (5.1.6) lib/active_record/validations.rb:82:in `perform_validations'
  activerecord (5.1.6) lib/active_record/validations.rb:44:in `save'
  activerecord (5.1.6) lib/active_record/attribute_methods/dirty.rb:35:in `save'
  activerecord (5.1.6) lib/active_record/transactions.rb:308:in `block (2 levels) in save'
  activerecord (5.1.6) lib/active_record/transactions.rb:384:in `block in with_transaction_returning_status'
  activerecord (5.1.6) lib/active_record/connection_adapters/abstract/database_statements.rb:233:in `transaction'
  activerecord (5.1.6) lib/active_record/transactions.rb:210:in `transaction'
  activerecord (5.1.6) lib/active_record/transactions.rb:381:in `with_transaction_returning_status'
  activerecord (5.1.6) lib/active_record/transactions.rb:308:in `block in save'
  activerecord (5.1.6) lib/active_record/transactions.rb:323:in `rollback_active_record_state!'
  activerecord (5.1.6) lib/active_record/transactions.rb:307:in `save'
  activerecord (5.1.6) lib/active_record/suppressor.rb:42:in `save'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/active_record_prepend.rb:16:in `block in save'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/transaction.rb:917:in `with_database_metric_name'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent.rb:618:in `with_database_metric_name'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/active_record_prepend.rb:15:in `save'
  activerecord (5.1.6) lib/active_record/associations/collection_association.rb:371:in `insert_record'
  activerecord (5.1.6) lib/active_record/associations/has_many_association.rb:34:in `insert_record'
  activerecord (5.1.6) lib/active_record/associations/collection_association.rb:360:in `block (2 levels) in _create_record'
  activerecord (5.1.6) lib/active_record/associations/collection_association.rb:447:in `replace_on_target'
  activerecord (5.1.6) lib/active_record/associations/collection_association.rb:281:in `add_to_target'
  activerecord (5.1.6) lib/active_record/associations/collection_association.rb:358:in `block in _create_record'
  activerecord (5.1.6) lib/active_record/associations/collection_association.rb:129:in `block in transaction'
  activerecord (5.1.6) lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
  activerecord (5.1.6) lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
  /usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
  activerecord (5.1.6) lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
  activerecord (5.1.6) lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
  activerecord (5.1.6) lib/active_record/transactions.rb:210:in `transaction'
  activerecord (5.1.6) lib/active_record/associations/collection_association.rb:128:in `transaction'
  activerecord (5.1.6) lib/active_record/associations/collection_association.rb:357:in `_create_record'
  activerecord (5.1.6) lib/active_record/associations/has_many_association.rb:121:in `_create_record'
  activerecord (5.1.6) lib/active_record/associations/association.rb:196:in `create'
  activerecord (5.1.6) lib/active_record/associations/collection_proxy.rb:347:in `create'
  actionpack (5.1.6) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
  actionpack (5.1.6) lib/abstract_controller/base.rb:186:in `process_action'
  actionpack (5.1.6) lib/action_controller/metal/rendering.rb:30:in `process_action'
  actionpack (5.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (5.1.6) lib/active_support/callbacks.rb:108:in `block in run_callbacks'
  activesupport (5.1.6) lib/active_support/core_ext/time/zones.rb:64:in `use_zone'
  browser-timezone-rails (1.0.1) lib/browser-timezone-rails.rb:15:in `set_time_zone'
  activesupport (5.1.6) lib/active_support/callbacks.rb:117:in `block in run_callbacks'
  activesupport (5.1.6) lib/active_support/callbacks.rb:135:in `run_callbacks'
  actionpack (5.1.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (5.1.6) lib/action_controller/metal/rescue.rb:20:in `process_action'
  actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
  activesupport (5.1.6) lib/active_support/notifications.rb:166:in `block in instrument'
  activesupport (5.1.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  activesupport (5.1.6) lib/active_support/notifications.rb:166:in `instrument'
  actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (5.1.6) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
  activerecord (5.1.6) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
  actionpack (5.1.6) lib/abstract_controller/base.rb:124:in `process'
  actionview (5.1.6) lib/action_view/rendering.rb:30:in `process'
  actionpack (5.1.6) lib/action_controller/metal.rb:189:in `dispatch'
  actionpack (5.1.6) lib/action_controller/metal.rb:253:in `dispatch'
  actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
  actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:31:in `serve'
  actionpack (5.1.6) lib/action_dispatch/journey/router.rb:50:in `block in serve'
  actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `each'
  actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `serve'
  actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:844:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/rack/agent_hooks.rb:30:in `traced_call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/rack/browser_monitoring.rb:32:in `traced_call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
  warden (1.2.7) lib/warden/manager.rb:35:in `catch'
  warden (1.2.7) lib/warden/manager.rb:35:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  rack (2.0.4) lib/rack/etag.rb:25:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  rack (2.0.4) lib/rack/conditional_get.rb:38:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  rack (2.0.4) lib/rack/head.rb:12:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  rack (2.0.4) lib/rack/session/abstract/id.rb:232:in `context'
  rack (2.0.4) lib/rack/session/abstract/id.rb:226:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/cookies.rb:613:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  activerecord (5.1.6) lib/active_record/migration.rb:556:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
  activesupport (5.1.6) lib/active_support/callbacks.rb:97:in `run_callbacks'
  actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  rollbar (2.15.5) lib/rollbar/middleware/rails/rollbar.rb:24:in `block in call'
  rollbar (2.15.5) lib/rollbar.rb:145:in `scoped'
  rollbar (2.15.5) lib/rollbar/middleware/rails/rollbar.rb:22:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
  rollbar (2.15.5) lib/rollbar/middleware/rails/show_exceptions.rb:22:in `call_with_rollbar'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  railties (5.1.6) lib/rails/rack/logger.rb:36:in `call_app'
  railties (5.1.6) lib/rails/rack/logger.rb:24:in `block in call'
  activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `block in tagged'
  activesupport (5.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `tagged'
  railties (5.1.6) lib/rails/rack/logger.rb:24:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  request_store (1.4.1) lib/request_store/middleware.rb:19:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/request_id.rb:25:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  rack (2.0.4) lib/rack/method_override.rb:22:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  rack (2.0.4) lib/rack/runtime.rb:22:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  activesupport (5.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  rack-rewrite (1.5.1) lib/rack/rewrite.rb:24:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  actionpack (5.1.6) lib/action_dispatch/middleware/static.rb:125:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  railties (5.1.6) lib/rails/engine.rb:522:in `call'
  newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
  puma (3.11.3) lib/puma/configuration.rb:225:in `call'
  puma (3.11.3) lib/puma/server.rb:624:in `handle_request'
  puma (3.11.3) lib/puma/server.rb:438:in `process_client'
  puma (3.11.3) lib/puma/server.rb:302:in `block in run'
  puma (3.11.3) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
  </code></pre>
  
  <p>This is the part which is repeating in the stack trace:</p>
  
  <blockquote>
    <p>activesupport (5.1.6) lib/active_support/callbacks.rb:422:in 'block in inverted_lambda'</p>
    
    <p>activesupport (5.1.6) lib/active_support/callbacks.rb:177:in 'block (2 levels) in halting_and_conditional'</p>
    
    <p>activesupport (5.1.6) lib/active_support/callbacks.rb:177:in 'each'</p>
    
    <p>activesupport (5.1.6) lib/active_support/callbacks.rb:177:in 'all?'</p>
    
    <p>activesupport (5.1.6) lib/active_support/callbacks.rb:177:in 'block in halting_and_conditional'</p>
    
    <p>activesupport (5.1.6) lib/active_support/callbacks.rb:507:in 'block in invoke_before'</p>
    
    <p>activesupport (5.1.6) lib/active_support/callbacks.rb:507:in 'each'</p>
    
    <p>activesupport (5.1.6) lib/active_support/callbacks.rb:507:in 'invoke_before'</p>
    
    <p>activesupport (5.1.6) lib/active_support/callbacks.rb:130:in 'run_callbacks'</p>
    
    <p>activesupport (5.1.6) lib/active_support/callbacks.rb:827:in '_run_validate_callbacks'</p>
    
    <p>activemodel (5.1.6) lib/active_model/validations.rb:405:in 'run_validations!'</p>
    
    <p>activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in 'block in run_validations!'</p>
    
    <p>activesupport (5.1.6) lib/active_support/callbacks.rb:131:in 'run_callbacks'</p>
    
    <p>activesupport (5.1.6) lib/active_support/callbacks.rb:827:in '_run_validation_callbacks'</p>
    
    <p>activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in 'run_validations!'</p>
    
    <p>activemodel (5.1.6) lib/active_model/validations.rb:335:in 'valid?'</p>
    
    <p>activerecord (5.1.6) lib/active_record/validations.rb:65:in 'valid?'</p>
  </blockquote>