简体   繁体   English

如何在ActiveRecord条件更新中保留新的对象层次结构?

[英]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.

相关问题 如果在Rails 3中nto等于,如何将activerecord对象与新对象进行比较并进行更新? - How to compare activerecord object to new object and update if nto equal in Rails 3? 如何用新属性扩展ActiveRecord模型? - How can I extend ActiveRecord model with new propery? 如何使用新方法扩展ActiveRecord关系? - How can I extend ActiveRecord relations with new method? 如何将多个相关对象分配给ActiveRecord中的对象 - How can I assign multiple related objects to an object in ActiveRecord 如何创建与ActiveRecord对象在一起的日志文件? - How can I create a log file that stays with an ActiveRecord object? 如何在运行时扩展从ActiveRecord关联返回的对象? - How can I extend an object returned from an ActiveRecord association at runtime? 如何将 object 转换为字符串以用于 ActiveRecord 查询? - How can I cast an object to string for use with ActiveRecord queries? 如何删除并返回ActiveRecord关系对象中的第一个元素? - How can I remove and return the first element in an ActiveRecord relation object? 如何打开一个类并将其转换为ActiveRecord对象? (例如,使其继承ActiveRecord :: Base) - How can I open a class and convert it into ActiveRecord object? (for example, make it inherit ActiveRecord::Base) 如何使用rspec测试条件ActiveRecord after_update回调? - How to test conditional ActiveRecord after_update callback with rspec?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM