Skip to content

Instantly share code, notes, and snippets.

@jasoncodes
Created October 18, 2011 05:42
Show Gist options
  • Save jasoncodes/1294696 to your computer and use it in GitHub Desktop.
Save jasoncodes/1294696 to your computer and use it in GitHub Desktop.
Using ActiveRecord optimistic locking with Inherited Resources
/ ...
- f.inputs do
= f.hidden_field :lock_version
/ ...
class AddLockingToFoo < ActiveRecord::Migration
def change
add_column :foos, :lock_version, :integer, :default => 0, :null => false
end
end
class FoosController < Admin::BaseController
inherit_resources
include UpdateLock
end
module UpdateLock
def update
raise ActiveRecord::MissingAttributeError, "Lock version missing" if params[resource_instance_name][:lock_version].blank?
super
rescue ActiveRecord::StaleObjectError
flash.now[:error] = t(
'flash.actions.update.stale',
:default => '%{resource_name} has been updated by another user.',
:resource_name => resource.class.human_name
)
render :edit
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment