How to skip transaction in ActiveRecord for INSERT ONLY statement?

前端 未结 5 994
孤独总比滥情好
孤独总比滥情好 2020-12-30 05:19

Look at this example:

2.1.3 :001 > Stat.create!
   (0.1ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `stats` (`created_at`, `updated_at`) VALUES (\'2015-03-16 11:         


        
5条回答
  •  孤城傲影
    2020-12-30 05:54

    Answer of Alejandro Babio is extensive but wanted to explain why transaction is done in the first place.

    This answer explains what role does the transaction have in the call. Here is it in short:

    begin transaction
    insert record
    after_save called
    commit transaction
    after_commit called
    

    But provided no after_save hook is registered by developer, I wonder why transaction is not skipped. For high latency connections, the transaction may increase overall operation time 3 times :/ IMO Rails needs to be optimized.

    Rails rejected such optimization, see why: https://github.com/rails/rails/issues/26272

提交回复
热议问题