From 4c5b73fef8a41bd2bd8435fa4b00f7c40b721650 Mon Sep 17 00:00:00 2001
From: Aaron Patterson <aaron.patterson@gmail.com>
Date: Thu, 19 Jan 2012 09:38:37 -0800
Subject: Merge pull request #4531 from exviva/pessimistic_with_lock

Add ActiveRecord::Base#with_lock
---
 railties/guides/source/active_record_querying.textile | 11 +++++++++++
 1 file changed, 11 insertions(+)

(limited to 'railties/guides/source/active_record_querying.textile')

diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile
index beada85ce3..5970a45839 100644
--- a/railties/guides/source/active_record_querying.textile
+++ b/railties/guides/source/active_record_querying.textile
@@ -692,6 +692,17 @@ Item.transaction do
 end
 </ruby>
 
+If you already have an instance of your model, you can start a transaction and acquire the lock in one go using the following code:
+
+<ruby>
+item = Item.first
+item.with_lock do
+  # This block is called within a transaction,
+  # item is already locked.
+  item.increment!(:views)
+end
+</ruby>
+
 h3. Joining Tables
 
 Active Record provides a finder method called +joins+ for specifying +JOIN+ clauses on the resulting SQL. There are multiple ways to use the +joins+ method.
-- 
cgit v1.2.3

