dpy*_*yro 5 ruby sqlite ruby-on-rails ruby-on-rails-4 ruby-2.1
CONFIGS/routes.rb中
Shutters和Paints是子资源Jobs.
resources :jobs do
    resources :shutters
    resources :paints
end
app/models/job.rb 
A 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 
A Shutter包含属于一个Job和一个Paint.
class Shutter < ActiveRecord::Base
    belongs_to :job
    belongs_to :paint
app/models/paint.rb 
A Paint属于一个,Job但Shutters在该工作中可被许多人引用.
class Paint < ActiveRecord::Base
    belongs_to :job
    has_many :shutters
无论Jobs#show和Jobs#edit做工精细的代码<%= debug @job.paints %>,如果没有Paints在数据库中已经.但是在添加绘制的那一刻,会引发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
dpy*_*yro 15
问题不在于这些.实际上我的Paint模型包含一个名为的字段:type,它是元分类的保留关键字或类似的东西.幸好得到了善良的帮助#RubyOnRails,否则像我这样的新手会尝试从头开始,遇到同样的问题,而且从来没有想过这个.希望这有助于未来的一些Google员工.支付它!
| 归档时间: | 
 | 
| 查看次数: | 12913 次 | 
| 最近记录: |