Your Question

Rails has with_lock feature , it mean

BEGIN
  SELECT * FROM {table_name} WHERE {pkey} = {pkey_value} FOR UPDATE
  // obj values renew by this select
  ......
COMMIT or ROLLBACK

and use gorm maybe like

  var item Item{}
  db.First(&item)
  // check status / validation here without lock first
  db.WithLock(&item , function(obj , tx *gorm.DB) error { // transaction wrapper this func first
    // obj has been re-fill values from SELECT FOR UPDATE
    // check status / validation here in row locking
    // update data here
  })

otherwise I think here is bad example of locking at doc https://gorm.io/docs/transactions.html

Expected answer

support Rails ActiveRecord.with_lock feature

Comment From: fatelei

https://gorm.io/docs/advanced_query.html#Locking-FOR-UPDATE

Comment From: JokerCatz

@fatelei yep , but I think it not a transaction block , it need

https://gorm.io/docs/advanced_query.html#Locking-FOR-UPDATE + https://gorm.io/docs/transactions.html#Transaction

so I think it easy to implement

Comment From: a631807682

Users may need to query tables that are not related to updates. Whether to lock or not should be determined by the user, this feature is not necessary, and you can implement it yourself.