[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.
[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.
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.