我想我是密集这里,是因为我不断收到一个错误stack too deep
...如何避免圆形环
我有一个Child
和Parent
关系对象。我想两件事情发生:
- ,如果你尝试更新
Child
,除非它有一个Parent
协会 - 如果你创建一个
Parent
,然后将其连接到Child
不能更新其status_id
到1
,那么Child
的状态应该自动设置为1
。
这里的Parent
协会如何得到补充说:
parent = Parent.new
if parent.save
child.update_attributes(parent_id:1)
end
我对Child
模型这些回调:
validate :mark_complete
after_update :set_complete
# this callback is here because there is a way to update the Child model attributes
def mark_complete
if self.status_id == 1 && self.parent.blank?
errors[:base] << ""
end
end
def set_complete
if self.logistic.present?
self.update_attribute(:status_id, 1)
end
end
上面的代码其实并不有效,因为它是2分贝命中当理想情况下它会是1,一次完成。但我发现它太耗费脑筋,不知道为什么......我不确定为什么它甚至没有工作,因此甚至无法考虑将其作为一个单独的数据库交易。
例
希望这有助于澄清。想象一下Charge
模型和Item
模型。每个Item
有一个Charge
。 Item
也有一个属性paid
。两件事情:
如果更新
Item
,你不能更新paid
到true
直到Item
已与Charge
对象如果通过更新链接
Charge
对象到Item
被相关charge_id
属性Item
,那么代码应该为您节省时间并自动设置为paid
为true
为什么你不直接循环孩子,直到父母结束? – 13aal