title: Circular dependency detected while autoloading constant (Rails 4, Ruby 2)
body: <p><strong>configs/routes.rb</strong>
  <code>Shutters</code> and <code>Paints</code> are subresources of <code>Jobs</code>.</p>
  
  <pre><code>resources :jobs do
      resources :shutters
      resources :paints
  end
  </code></pre>
  
  <p><strong>app/models/job.rb</strong>
  A <code>Job</code> contains many <code>Shutters</code> and many <code>Paints</code>.</p>
  
  <pre><code>class Job &lt; ActiveRecord::Base
      has_many :shutters, dependent: :delete_all
      has_many :paints, dependent: :delete_all
  
      accepts_nested_attributes_for :shutters, allow_destroy: true, :reject_if =&gt; lambda { |a| a[:no].blank? }
      accepts_nested_attributes_for :paints, allow_destroy: true, :reject_if =&gt; lambda { |a| a[:name].blank? }`
  </code></pre>
  
  <p><strong>app/models/shutter.rb</strong>
  A <code>Shutter</code> contains belongs to one <code>Job</code> and one <code>Paint</code>.</p>
  
  <pre><code>class Shutter &lt; ActiveRecord::Base
      belongs_to :job
      belongs_to :paint
  </code></pre>
  
  <p><strong>app/models/paint.rb</strong>
  A <code>Paint</code> belongs to one <code>Job</code> but can be referenced by many <code>Shutters</code> in that job.</p>
  
  <pre><code>class Paint &lt; ActiveRecord::Base
      belongs_to :job
      has_many :shutters
  </code></pre>
  
  <p>Both <code>Jobs#show</code> and <code>Jobs#edit</code> work fine with the code <code>&lt;%= debug @job.paints %&gt;</code> if there are no <code>Paints</code> already in the database. But the moment a paint is added, a RuntimeError is raised, "Circular dependency detected while autoloading constant PAINT".</p>
  
  <p>What's the best way to fix this error?</p>
  
  <p><strong>Edit:</strong> Controller info</p>
  
  <p><strong>app/controllers/jobs_controller.rb</strong></p>
  
  <pre><code>class JobsController &lt; ApplicationController
      before_action :set_job, only: [:show, :edit, :update, :destroy]
  
      ...
  
      # GET /jobs/1
      # GET /jobs/1.json
      def show
      end
  
      # GET /jobs/new
      def new
          @job = Job.new
          @job.shutters.build
          @job.paints.build
      end
  
      # GET /jobs/1/edit
      def edit
          @job.shutters.build
          @job.paints.build
      end
  </code></pre>
  
  <p><strong>app/controllers/shutters_controller.rb</strong></p>
  
  <pre><code>class ShuttersController &lt; ApplicationController
      def new
          @shutter = Shutter.new
      end
  
      def destroy
          @shutter = Shutter.find(params[:id])
          @job = Job.find(@shutter[:job_id])
          @shutter.destroy
          respond_to do |format|
              format.html {redirect_to @job, notice: 'Shutter was succesfully deleted.' }
          end
      end
  end
  </code></pre>
  
  <p><strong>app/controllers/paints_controller.rb</strong></p>
  
  <pre><code>class PaintsController &lt; ApplicationController
      def new
          @paint = Paint.new
      end
  
      def destroy
          @paint = Paint.find(params[:id])
          @job = Job.find(@paint[:job_id])
          @paint.destroy
          respond_to do |format|
              format.html {redirect_to @job, notice: 'Paint was succesfully deleted.' }
          end
      end
  end
  </code></pre>