Ruby Forum Ruby on Rails > Need Explainantion of ActiveRecord::Base.transaction

Posted by Rails Terrorist (malioboro)
on 07.05.2008 04:28
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 by Frederick Cheung (Guest)
on 07.05.2008 10:42
(Received via mailing list)
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
Posted by Rails Terrorist (malioboro)
on 07.05.2008 12:23
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 by Frederick Cheung (Guest)
on 07.05.2008 13:03
(Received via mailing list)
On 7 May 2008, at 11:23, Rails Terrorist wrote:

> 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
Posted by AndyV (Guest)
on 07.05.2008 16:47
(Received via mailing list)
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...@gmail.com>
Posted by Rails Terrorist (malioboro)
on 07.05.2008 18:37
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