Ruby on Rails
Tuesday, December 3, 2013
Hello,
-- I also has this problem, I need to create an AuditTrail instance in db even if the transition is failed, but if the transition failed, the transaction is rollback, and the failed AuditTrail instance is not created in database.
On Wednesday, January 27, 2010 12:55:46 PM UTC+8, FFighter wrote:
On Wednesday, January 27, 2010 12:55:46 PM UTC+8, FFighter wrote:
Hello list,
I have a subscription model that has a upgrade event. This changes the state from free->paying and triggers a charge method in the subscription model. This charge method then creates a new instance of the payment model (just in memory, using .new), and triggers the pay event on it. The pay event tries to transition the payment from pending to paid and calls the do_pay method before the transition (before_transition).
Everything would be 100% if it weren't for a small details. The payment should be persisted even if the subscription transition from free->paying has failed. In the do_pay method, if the payment failed, I do self.decline (a FSM event that simply changes from pending->declined) and I have an after_transition callback setup like: "after_transition any => any, do {|p| p.save }" which gets triggered. However, since I return false in the do_pay method to prevent the pendign->paid transition to complete (the payment failed, remember?), state_machine then rollbacks the payment's save.
What I did was to call:
self.class.connection.commit_db_transaction
after self.decline and before return false in the payment's do_pay method. This works, but is kind of ugly.
If anyone out there has any suggestions on how I could improve this design or maybe just tell me that this is ok to do, alleviating my worries about what seems to be a hack, I would be grateful!
Thanks,
Marcelo.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/94d1b4b4-f7e2-413e-baf2-e13d55138e3d%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment