ruby-on-rails – 两个模型相互接受嵌套属性,生成堆栈级别太深
发布时间:2020-12-17 02:34:28 所属栏目:百科 来源:网络整理
导读:我想知道我是否只是这个问题的那个人.为了确定它,我创建了两个非常基本的模型. # user.rbclass User ActiveRecord::Base has_one :role,:inverse_of = :user accepts_nested_attributes_for :roleend# role.rbclass Role ActiveRecord::Base belongs_to :use
我想知道我是否只是这个问题的那个人.为了确定它,我创建了两个非常基本的模型.
# user.rb class User < ActiveRecord::Base has_one :role,:inverse_of => :user accepts_nested_attributes_for :role end # role.rb class Role < ActiveRecord::Base belongs_to :user,:inverse_of => :role accepts_nested_attributes_for :user end 在rails控制台中,如果已加载User模型,则尝试更新Role类的简单属性将失败. Loading development environment (Rails 3.2.2) 1.9.3-p194 :001 > Role.first.update_attribute(:role_type,72) => true 1.9.3-p194 :002 > Role.first.tap {|r| r.user}.update_attribute(:role_type,72) SystemStackError: stack level too deep from /Users/enelson/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/workspace.rb:80 Maybe IRB bug! 如果我删除其中一个accepts_nested_attributes指令,这个问题就会消失.我想这里简单的答案是“那么,你为什么不摆脱其中一个呢?”问题是涉及重写应用程序中的大量现有页面,如果有不同的修复或解决方法,我想听听它. 解决方法
在设计方面,角色“拥有”用户是不正确的构成.在这个阶段纠正设计可能更好,而不是留下它.
两种关系也被定义为另一种关系的“逆”;这也可能有助于无限递归. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |