我有一个非常基本的例子
应用/模型/ user.rb
#name string
class User < ActiveRecord::Base
has_one :project,dependent: :destroy
validates :name,presence: true
validates_associated :project
accepts_nested_attributes_for :project
end
应用/模型/ project.rb
#name string
#user_id integer
class Project < ActiveRecord::Base
belongs_to :user
has_many :tasks,dependent: :destroy
validates :name,presence: true
validates_associated :tasks
accepts_nested_attributes_for :tasks
end
应用/模型/ tasks.rb
#name string
#project_id integer
class Task < ActiveRecord::Base
belongs_to :project,dependent: :destroy
validates :name,presence: true
end
这就是我在模特中所拥有的一切
在Controller端,测试目的,我有以下代码。
应用/控制器/ users_controller.rb
def update
@user.project_attributes = {:name => "P#rand(100)",:tasks_attributes => [{name: "Task#{rand(100)}"}]}
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
每次我尝试更新给定的用户,我都会遇到stack level too deep
问题
任何线索?
Rails version 4.0.4
Ruby 2.1.2p95
答案 0 :(得分:4)
仅在共享关联的一个模型中写入dependent: :destroy
。如果两个模型中都有{{1}},则会发生无数次调用,导致堆栈级别太深错误。