有没有更好的方法来编写这段代码?它只是不适合我,我觉得有一些我应该已经知道的真正“轨道式”的东西:
belongs_to :parent_in_use
belongs_to :parent_template
def after_create
new_parent_in_use = ParentInUse.create!(self.parent_template.attributes)
self.update_attribute(:parent_in_use_id, new_parent_in_use.id)
end
创建记录后,我将使用选定的父模板并parent_in_use
基于它创建记录。这样模板可以更改,in_use 记录将永远与我的对象一起存在。ParentInUse 和 ParentTemplate 类都使用 STI 从 Parent 继承。
我确定这应该足够简单,但我不知道该怎么做,基本上我想在一次操作中创建并分配记录。