Rails Terrorist
2008-May-07 02:28 UTC
Need Explainantion of ActiveRecord::Base.transaction
Dear All: Any one can help me to explain what is it for: [1] ActiveRecord::Base.transaction and Customer.transaction ? [2] What is deferences ActiveRecord::Base.transaction and Customer.transaction ? [3] Is it the same like Transaction Support? [4] Any one can give me proof of concept of ActiveRecord::Base.transaction ? Thank you very much for your time and help. Best Regards, Reinhart -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-May-07 08:41 UTC
Re: Need Explainantion of ActiveRecord::Base.transaction
On 7 May 2008, at 03:28, Rails Terrorist wrote:> > Dear All: > > Any one can help me to explain what is it for: > > [1] ActiveRecord::Base.transaction and Customer.transaction ?Most of the time they are identical. They''re different if Customer.connection != ActiveRecord::Base.connection> [2] What is deferences ActiveRecord::Base.transaction and > Customer.transaction ?No idea what you are talking about> [3] Is it the same like Transaction Support? >they are for database transactions> [4] Any one can give me proof of concept of > ActiveRecord::Base.transaction ? >Customer.transaction do #do something end Fred> Thank you very much for your time and help. > > > Best Regards, > Reinhart > -- > 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 -~----------~----~----~----~------~----~------~--~---
Rails Terrorist
2008-May-07 10:23 UTC
Re: Need Explainantion of ActiveRecord::Base.transaction
Thank You Fred, I need to know : [1] When do I use Model_name.transaction and When do I use ActiveRecord::Base.transaction? [2] I have related tables in database and they are depended one each other. And they need having transaction support in a method. Which is good ActiveRecord::Base.transaction or each name_model.transaction ? [3] Any good reference article that can explain it detail? I cant get it in google. Only sample code, but not explain process in transaction. Once Again, Thank you very much. Reinhart -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-May-07 11:02 UTC
Re: Need Explainantion of ActiveRecord::Base.transaction
On 7 May 2008, at 11:23, Rails Terrorist wrote:> > Thank You Fred, > > I need to know : > > [1] When do I use Model_name.transaction and When do I use > ActiveRecord::Base.transaction? > > [2] I have related tables in database and they are depended one each > other. And they need having transaction support in a method. Which is > good ActiveRecord::Base.transaction or each name_model.transaction ? > >Unless you''re playing with multiple databases they''re all the same> [3] Any good reference article that can explain it detail? I cant > get it > in google. Only sample code, but not explain process in transaction.Don''t know about that. there''s not really an awful lot to say, it''s just a means of running some code inside a database transaction. If an exception is thrown it''s rolled back, if not it''s committed (there''s a specific exception you can throw to just roll back, can''t remember the name but it will be in the docs. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Unless you do something specific to override the default functionality, a model that inherits from ActiveRecord::Base inherits the db connection that it uses. Indirectly, this means that ALL the models that inherit from ARec::Base will use the same connection (again, barring a specific override). The transaction method, as Fred explains, provides an explicit way to mark the beginning and end of a db transaction. The transaction is run in the context of the db connection. If you have not done anything to override the way that your model sets/gets its DB connection then there is absolutely no difference between using transaction on ARec::Base or any of the models that inherited from it. On May 7, 7:02 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 7 May 2008, at 11:23, Rails Terrorist wrote: > > > > > Thank You Fred, > > > I need to know : > > > [1] When do I use Model_name.transaction and When do I use > > ActiveRecord::Base.transaction? > > > [2] I have related tables in database and they are depended one each > > other. And they need having transaction support in a method. Which is > > good ActiveRecord::Base.transaction or each name_model.transaction ? > > Unless you''re playing with multiple databases they''re all the same > > > [3] Any good reference article that can explain it detail? I cant > > get it > > in google. Only sample code, but not explain process in transaction. > > Don''t know about that. there''s not really an awful lot to say, it''s > just a means of running some code inside a database transaction. If an > exception is thrown it''s rolled back, if not it''s committed (there''s > a specific exception you can throw to just roll back, can''t remember > the name but it will be in the docs. > > Fred--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rails Terrorist
2008-May-07 16:37 UTC
Re: Need Explainantion of ActiveRecord::Base.transaction
Thanks guys, I understand. But example if i have more than 1 models for transaction, could i use ActiveRecord::Base.transaction like sample below: ActiveRecord::Base.transaction do @lecturer_A = AlphaDepartment.new(params[:alpha_lecturer]) if @lecturer_A.save @lecturer_A_in_B = BetaDepartment.find_by_ssn(@lecturer_A.ssn) @lecturer_A_in_D = DeltaDepartment.find_by_ssn(@lecturer_A.ssn) if @lecturer_A_in_B.start_work < 5.years @lecturer_A_in_B.histories.delete_all @lecturer_A_in_B.histories << @lecturer_A_in_D.histories else @lecturer_A.histories << @lecturer_A_in_B.histories end end end What should I use AlphaDepartment.transaction, BetaDepartment.transaction & DeltaDepartment.transaction together in 1 action like above? any suggestion? Thank You, Reinhart -- 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 -~----------~----~----~----~------~----~------~--~---