I've been using the actsasrevisable plugin in my Rails apps to store a revision history of ActiveRecord models. The plugin automatically versions your models, and allows you to navigate revisions; branch and merge changes, and perform bulk changesets.
During some tests of my code, I found a conflict between the plugin and
ActiveRecord.validates_uniqueness_of. It seems the conflict occurs because actsasrevisable stores all revisions in the same database table. For example, when trying to save a record whose
:name attribute should be unique, the validator will see all previous versions of the record with identical names, and prevent the save.
The fix I put in for this was to use the
:scope to scope
validates_uniqueness_of by the
```rubyclass User < ActiveRecord::Base
validatesasunique :username, :scope => :revisableiscurrent
Scoping the attribute causes the validator to only consider records whose
:revisable_is_current attribute matches that of the record being edited. This works for my app, as only the current revision (
User#revisable_is_current= true) will ever be edited.
Are you using actsasrevisable and come across this problem? Spotted a better solution? Feel free to discuss ideas in the comments.