自动加载常量时检测到循环依赖(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 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
。
class Shutter < ActiveRecord::Base belongs_to :job belongs_to :paint
app / models / paint.rb一个Paint
属于一个Job
但可以被该作业中的许多Shutters
引用。
class Paint < ActiveRecord::Base belongs_to :job has_many :shutters
如果数据库中已有Paints
,那么Jobs#show
和Jobs#edit
可以正常使用代码 。 但是在添加绘制的那一刻,会引发RuntimeError,“在自动加载常量PAINT时检测到循环依赖性”。
修复此错误的最佳方法是什么?
编辑:控制器信息
应用程序/控制器/ 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
应用程序/控制器/ 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
应用程序/控制器/ 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
帮助,否则像我这样的新手会尝试从头开始,遇到同样的问题,而且从来没有想过这个。 希望这有助于未来的一些Google员工。 支付它!