Does user 37 have the ability to change his own cluster? If not, then you’ll only update the fields that your form submits… not everything. (provided you use update_attributes to save the results)
If he can update his cluster, then the only thing that comes to mind is to look at the modified date/time of the record you have out and compare it to what’s in the database.
Add a field to your table called updated_at
then in your User model, do a callback
before_update :check_if_stale
protected
def check_if_stale
user_updated_at = User.find(
self.id).updated_at
if user_updated_at == self.updated_at
#record is ok to update
else
# your copy is stale… better do some corrective stuff… could return false which would prevent the record from being saved.
end
end
Not sure if that’s the best approach… someone else could have a cleaner way of doing that.
Yeah, I don't want to force all the system to use update_attributes though, sounds brittle. I prefer a solution that still lets people use AR models normally (except for non-bang-saves, who can raise now an exception as save! does).
Too quick, since that field is changed by an update_all lock_version is untouched. I could do a select+assign in a before_save but there's still room for a race condition there, hmmmmm.
Your original update_all is sufficient if you include lock_version=lock_version+1. Then dirty readers will get a StaleObjectError if they save after the cluster change.