1

I tried to find on internet, but I could find a good explanation. What does lock ending with exclamation mark mean in ruby? e.g. @student.lock! (BTW, I know what lock means in concurrency)

Leem.fin
  • 35,699
  • 70
  • 166
  • 297
  • 1
    No special meaning. Exclamation mark is part of the method name. Look up method's documentation to find out what it does. – Sergio Tulentsev Jun 25 '19 at 10:30
  • 1
    In Active Record, `lock` and `lock!` are not the same. The former exists on relations, the latter on records. – fphilipe Jun 25 '19 at 10:34
  • "I tried to find on internet, but I could find a good explanation" – It would be helpful if you could explain what *exactly* is missing in the documentation. That way, the Rails developers can improve the documentation so that future developers don't stumble across the same problems as you did. You would basically make the world a better place, and who wouldn't want that? – Jörg W Mittag Jun 29 '19 at 05:59

1 Answers1

0

Go to the Ruby on Rails site. Click on the "API" link at the top. Type "lock!" into the search field. Click on the first result.

ActiveRecord::Locking::Pessimistic#lock!

lock!(lock = true)

Obtain a row lock on this record. Reloads the record to obtain the requested lock. Pass an SQL locking clause to append the end of the SELECT statement or pass true for “FOR UPDATE” (the default, an exclusive row lock). Returns the locked record.

If you're unclear on what a row lock is, the first half of this article is a good place to start. The TL;DR is that a row lock prevents some other database user (which might be another connection from the same app) from modifying the row you're modifying or about to modify.

Jordan Running
  • 91,621
  • 15
  • 164
  • 165