# Rename lock\_version field?

**URL:** <https://www.ruby-forum.com/t/rename-lock-version-field/51643>\
**Category:** Rails\
**Created:** [January 5, 2006, 12:37am UTC](https://www.ruby-forum.com/t/rename-lock-version-field/51643 "2006-01-05T00:37:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phersh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@phersh](https://www.ruby-forum.com/u/phersh)\
**Post date:** [January 5, 2006, 12:37am UTC](https://www.ruby-forum.com/t/rename-lock-version-field/51643/1 "2006-01-05T00:37:50Z")

</div>

Hi,

Is there a way to tell Rails that the magic field name for optimistic  
database locking is not lock\_version? I have a legacy database where the  
optimistic locking field is named “version”.

Thanks,  
Phil

---

<div class="post-metadata">

**Author:** ![phersh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@phersh](https://www.ruby-forum.com/u/phersh)\
**Post date:** [January 5, 2006, 12:53am UTC](https://www.ruby-forum.com/t/rename-lock-version-field/51643/2 "2006-01-05T00:53:16Z")

</div>

Phillip H. wrote:

> Hi,
> 
> Is there a way to tell Rails that the magic field name for optimistic  
> database locking is not lock\_version? I have a legacy database where the  
> optimistic locking field is named “version”.
> 
> Thanks,  
> Phil

Hi Phil,

To do what you suggest, you would begin by modifying

C:\ruby\lib\ruby\gems\1.8\gems\activerecord-1.13.2\lib\active\_record\locking.rb

replace every occurance of “lock\_version” with “version”

Beyond this, I am sure there must be other places where you must do the  
same thing, but I’ve not taken the time to look into it further.

Ideally, things like this should be defineable somehow, like

#define lock\_version\_field\_name = “version”

but this is not something readyli available in rails. At least not that  
I know of (and I don’t know all that much).

Good luck.

-John-

---

<div class="post-metadata">

**Author:** ![phersh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@phersh](https://www.ruby-forum.com/u/phersh)\
**Post date:** [January 5, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/rename-lock-version-field/51643/3 "2006-01-05T21:03:37Z")

</div>

On 1/4/06, Phillip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> Is there a way to tell Rails that the magic field name for optimistic  
> database locking is not lock\_version? I have a legacy database where the  
> optimistic locking field is named “version”.
> 
> Thanks,  
> Phil

I just made a patch against the stable version of Rails that lets you  
set the name of the locking column on your model classes.

Example:

class LegacyThing \< ActiveRecord::Base  
set\_locking\_column :version  
end

If you don’t specify a column name, it defaults to lock\_version. So  
far I’ve only tested it against Oracle, but it basically doesn’t  
change the SQL code, so you should be fine with other DBMSes.

The patch is against revision 3381, the latest stable version, but it  
should apply against anything, since the locking.rb file hasn’t been  
changed in 6 months.

Hopefully this will help you out. 🙂

–Wilson.
