Ruby-kai in Kyoto
Today is already too late, so just some notes. Need more research on "Transactions in Rails" (Takagi-san):
Save and destroy are automatically wrapped in a transaction
When you use the Transactions API, the system is really just using the underlying database SQL BEGIN...COMMIT. For MySQL, this means using InnoDB engine.
- Using transactions between two or more tables
A transaction acts on a single database connection. If you have multiple class-specific databases, the transaction will not protect interaction among them. One workaround is to begin a transaction on each class whose models you alter:
Account.transaction do User.transaction do yada_yada end end
1. Switch all tables to InnoDB
alter table table_as engine=innodb
2. In the controller:
def insert_big_form TableA.transaction do ta = TableA.create({params[:table_a]}) td = TableD.new({params[:table_d]}) params[:table_c]["table_d"] = td ta.table_cs << TableC.new({:table_d => td, params[:table_c]}) ta.table_b = TableB.new(params[:table_b]) ta.save end end
When shouldn't you use Rails? If you need to handle transactions across multiple databases. Rails/Ruby doesn't have two phase commit (yet).