How do I create a new instance of a different class when saving a model
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
精彩评论