Created
October 18, 2011 05:42
-
-
Save jasoncodes/1294696 to your computer and use it in GitHub Desktop.
Using ActiveRecord optimistic locking with Inherited Resources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ ... | |
- f.inputs do | |
= f.hidden_field :lock_version | |
/ ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddLockingToFoo < ActiveRecord::Migration | |
def change | |
add_column :foos, :lock_version, :integer, :default => 0, :null => false | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FoosController < Admin::BaseController | |
inherit_resources | |
include UpdateLock | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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