自动加载常量时检测到循环依赖(Rails 4,Ruby 2) [英] Circular dependency detected while autoloading constant (Rails 4, Ruby 2)
问题描述
configs/routes.rb
Shutters
和Paints
是Jobs
的子资源.
resources :jobs do
resources :shutters
resources :paints
end
app/models/job.rb
Job
包含许多Shutters
和许多Paints
.
class Job < ActiveRecord::Base
has_many :shutters, dependent: :delete_all
has_many :paints, dependent: :delete_all
accepts_nested_attributes_for :shutters, allow_destroy: true, :reject_if => lambda { |a| a[:no].blank? }
accepts_nested_attributes_for :paints, allow_destroy: true, :reject_if => lambda { |a| a[:name].blank? }`
app/models/shutter.rb
一个Shutter
包含属于一个Job
和一个Paint
.
app/models/shutter.rb
A Shutter
contains belongs to one Job
and one Paint
.
class Shutter < ActiveRecord::Base
belongs_to :job
belongs_to :paint
app/models/paint.rb
一个Paint
属于一个Job
,但是在该作业中可以被许多Shutters
引用.
app/models/paint.rb
A Paint
belongs to one Job
but can be referenced by many Shutters
in that job.
class Paint < ActiveRecord::Base
belongs_to :job
has_many :shutters
如果数据库中没有Paints
,则Jobs#edit
都可以与代码<%= debug @job.paints %>
一起正常工作.但是,一旦添加涂料,就会引发RuntimeError,在自动加载常量PAINT时检测到循环依赖".
Both Jobs#show
and Jobs#edit
work fine with the code <%= debug @job.paints %>
if there are no Paints
already in the database. But the moment a paint is added, a RuntimeError is raised, "Circular dependency detected while autoloading constant PAINT".
解决此错误的最佳方法是什么?
What's the best way to fix this error?
控制器信息
app/controllers/jobs_controller.rb
class JobsController < 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
app/controllers/shutters_controller.rb
class ShuttersController < 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
app/controllers/paints_controller.rb
class PaintsController < 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
推荐答案
问题不是这些.实际上,我的Paint
模型包含一个名为:type
的字段,这是元分类或类似内容的保留关键字.幸好得到了#RubyOnRails
同事的帮助,否则像我这样的新手将尝试从头开始,遇到相同的问题,并且永远都不会想到.希望这对将来的Googler有帮助.向前付款!
The problem was none of these. It was actually that my Paint
model contained a field named :type
, which is a reserved keyword for meta-classing or something as such. Was thankfully helped by the kind folks at #RubyOnRails
, else a newbie like me would have tried to start from scratch, run into the same problem, and would have never have figured this out. Hopefully this helps some future Googler. Pay it forward!
这篇关于自动加载常量时检测到循环依赖(Rails 4,Ruby 2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!