在Rails ActiveRecord模型中创建父级时创建子对象的当前最佳实践是什么?

时间:2022-11-28 08:45:13

I would like to create a child object any time an instance of the parent is created.

我想在创建父实例的任何时候创建子对象。

Thus calling Parent.new() will automatically generate a child object which is associated with the parent. I override the initializer (as below) but is there a better way? I need to pass in one of the parameters to the child object but I don't want to use nested attributes. It's for an API and the split should be invisible for the API users.

因此,调用Parent.new()将自动生成与父对象关联的子对象。我重写初始化器(如下所示),但有更好的方法吗?我需要将其中一个参数传递给子对象,但我不想使用嵌套属性。它适用于API,并且对于API用户而言,拆分应该是不可见的。

has_one :child
validates_associated :child

def initializer(args*)
   @child = Child.new(args[:some_argument])
   super
end

2 个解决方案

#1


2  

I think that's why ActiveRecord has callbacks like: after_create and after_save.

我认为这就是ActiveRecord有回调的原因:after_create和after_save。

For what it's worth I know a famous open source project named Spree which has product and master variant(a child model: Variant) as dependency for a Product object.

值得一提的是,我知道一个名为Spree的着名开源项目,它将产品和主变量(子模型:Variant)作为Product对象的依赖项。

Here's the code: https://github.com/spree/spree/blob/master/core/app/models/spree/product.rb#L236-L250

这是代码:https://github.com/spree/spree/blob/master/core/app/models/spree/product.rb#L236-L250

and: https://github.com/spree/spree/blob/master/core/app/models/spree/product.rb#L87

和:https://github.com/spree/spree/blob/master/core/app/models/spree/product.rb#L87

You can have an after_create to save the child object data.

您可以使用after_create来保存子对象数据。

And, in my opinion. You shouldn't be overriding initialize for models. You may end up running into circular dependency issues or some random bug later. Just saying..

而且,在我看来。您不应该重写模型的初始化。您最终可能会遇到循环依赖问题或一些随机错误。只是说......

#2


1  

Another option might be using after_initialize callback:

另一种选择可能是使用after_initialize回调:

after_initialize -> { child = Child.new(child_attributes) }

after_initialize - > {child = Child.new(child_attributes)}

This would mean you'd have to also set attr_accessor for a parent object:

这意味着您还必须为父对象设置attr_accessor:

class Parent
  attr_accessor :child_attributes
end

This is just a different approach and it doesn't really look better to me. I don't think rails has anything OFTB for this case besides of nested_attributes

这只是一种不同的方法,对我来说并不是真的好看。除了nested_attributes之外,我不认为rails对于这种情况有任何OFTB

#1


2  

I think that's why ActiveRecord has callbacks like: after_create and after_save.

我认为这就是ActiveRecord有回调的原因:after_create和after_save。

For what it's worth I know a famous open source project named Spree which has product and master variant(a child model: Variant) as dependency for a Product object.

值得一提的是,我知道一个名为Spree的着名开源项目,它将产品和主变量(子模型:Variant)作为Product对象的依赖项。

Here's the code: https://github.com/spree/spree/blob/master/core/app/models/spree/product.rb#L236-L250

这是代码:https://github.com/spree/spree/blob/master/core/app/models/spree/product.rb#L236-L250

and: https://github.com/spree/spree/blob/master/core/app/models/spree/product.rb#L87

和:https://github.com/spree/spree/blob/master/core/app/models/spree/product.rb#L87

You can have an after_create to save the child object data.

您可以使用after_create来保存子对象数据。

And, in my opinion. You shouldn't be overriding initialize for models. You may end up running into circular dependency issues or some random bug later. Just saying..

而且,在我看来。您不应该重写模型的初始化。您最终可能会遇到循环依赖问题或一些随机错误。只是说......

#2


1  

Another option might be using after_initialize callback:

另一种选择可能是使用after_initialize回调:

after_initialize -> { child = Child.new(child_attributes) }

after_initialize - > {child = Child.new(child_attributes)}

This would mean you'd have to also set attr_accessor for a parent object:

这意味着您还必须为父对象设置attr_accessor:

class Parent
  attr_accessor :child_attributes
end

This is just a different approach and it doesn't really look better to me. I don't think rails has anything OFTB for this case besides of nested_attributes

这只是一种不同的方法,对我来说并不是真的好看。除了nested_attributes之外,我不认为rails对于这种情况有任何OFTB