Is there a way to use the transaction method in a controller class? In a controller method, I would like to save the user model objects and several other model objects in the database in a transaction: either save all or none. This can be done in the model class but ActiveRecord::Base.transaction as the full name indicates is is not available in ApplicationController . Is there a way to call the transaction method in a controller class? Thanks much.
> ActiveRecord::Base.transaction as the full name indicates is is not > available in ApplicationController .Did not get the above statement..Anyway you can do like def controller_action begin ActiveRecord::Base.transaction do ---------your all transactions here end rescue ActiveRecord::ActiveRecordError => e: ---------- rescue code here else ----------in case nothing raised end end But better to move this to model so to make controller skinny Sijo -- Posted via http://www.ruby-forum.com/.
Thanks, Sijo. That works. I would like to move this logic down to the model level but I can''t because the transaction involves different models. Any suggestions? Thanks. On Jul 7, 2:31 am, Sijo Kg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > ActiveRecord::Base.transaction as the full name indicates is is not > > available in ApplicationController . > > Did not get the above statement..Anyway you can do like > > def controller_action > > begin > ActiveRecord::Base.transaction do > ---------your all transactions here > end > rescue ActiveRecord::ActiveRecordError => e: > ---------- rescue code here > else > ----------in case nothing raised > end > > end > > But better to move this to model so to make controller skinny > > Sijo > -- > Posted viahttp://www.ruby-forum.com/.
Learn by Doing wrote:> Thanks, Sijo. That works. > > I would like to move this logic down to the model level but I can''t > because the transaction involves different models. Any suggestions? > Thanks.Create a non-AR model to handle the transaction dispatch. Sijo is quite right that this does not belong in the controller. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Thanks Marnen! What do you mean by a non-AR model? I have users and each user has a spec. I''d like to do something like this transaction do user.save! spec.save! end Thanks. On Fri, Jul 17, 2009 at 11:53 AM, Marnen Laibow-Koser < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Learn by Doing wrote: > > Thanks, Sijo. That works. > > > > I would like to move this logic down to the model level but I can''t > > because the transaction involves different models. Any suggestions? > > Thanks. > > Create a non-AR model to handle the transaction dispatch. Sijo is quite > right that this does not belong in the controller. > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Learn by Doing wrote:> Thanks Marnen! What do you mean by a non-AR model?I mean a model class that does not inherit from ActiveRecord.> I have users and > each > user has a spec. I''d like to do something like this > > transaction do > user.save! > spec.save! > endRight. Put that in a method of your new class, then call it from the controller.> > Thanks. >Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
I see. Thanks, Marnen! On Fri, Jul 17, 2009 at 12:04 PM, Marnen Laibow-Koser < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Learn by Doing wrote: > > Thanks Marnen! What do you mean by a non-AR model? > > I mean a model class that does not inherit from ActiveRecord. > > > I have users and > > each > > user has a spec. I''d like to do something like this > > > > transaction do > > user.save! > > spec.save! > > end > > Right. Put that in a method of your new class, then call it from the > controller. > > > > > Thanks. > > > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---