[英]How can I preserve a new object hierarchy in an ActiveRecord conditional update?
I'm building a small AR object hierarchy, say a Parent
which has_many
Child
objects, instantiating and associating each as I go. 我正在构建一个小的AR对象层次结构,比如说一个具有has_many
Child
对象的Parent
,在我进行过程中实例化和关联每个对象。
Now that I have that, I need to check whether the Parent
already exists before saving, and if it does then update the entire hierarchy with potentially new values for each attribute of every object. 有了这个功能之后,我需要在保存之前检查Parent
是否已经存在,如果确实存在,则使用每个对象的每个属性的潜在新值更新整个层次结构。
I can't find_or_create
first. 我不能先find_or_create
。 I have to build the hierarchy and then do the check and create or update. 我必须建立层次结构,然后进行检查并创建或更新。
How does Rails handle that? Rails如何处理呢? If I only had the parent it would be much simpler, but all the attached objects make it more difficult. 如果我只有父母,那会简单得多,但是所有附加的对象都会使它更加困难。
I think, you can manage this through nested form and it is useful to store the parent/child relation data. 我认为,您可以通过嵌套形式进行管理,并且存储父/子关系数据非常有用。
Hope it is helpful for you!! 希望对您有帮助!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.