开发者

How do I create a new instance of a different class when saving a model

开发者 https://www.devze.com 2022-12-24 20:40 出处:网络
I have a Class A that when it is instantiated and saved for the first time or modified it will create an instance of Class B and save it also

I have a Class A that when it is instantiated and saved for the first time or modified it will create an instance of Class B and save it also

I want them to be in the same transaction and I want it to be handled in the Model not a controller.

I know how to do this in a conteoller with the ‘transaction do’ block but how do I do it 开发者_如何学Cin the model?

Thanks


Do it in an after_save callback, which is in the same transaction as the original save. See the Rails guides.


use models callbacks such as before_save or before_create etc

http://apidock.com/rails/ActiveRecord/Callbacks

0

精彩评论

暂无评论...
验证码 换一张
取 消