I have a model which both have optimistic locking and a counter. Is
there any way to disable the version auto increment when only the
counter is updated?
Lets say I have a post which has many comments. When I add a comment I
don't want the version_lock to be incremented. This happens now because
the post has a comments_count column.
Why do you care about version_lock value? That field is really for the AR “internal recordkeeping” part of optimistic locking.
In .NET, this type of control is handled with a GUID, just to point out that the field value itself is useless outside of the scope of the locking mechanism.
This is because I in my edit view of the post also have a control panel
for managing the comments (ajax). If a comment here is removed (without
reloading the page and then also the post) the posts counter cache is
updated alongside with its lock_version. If I then try to save the post
its stale (StaleObjectError).
So I'm looking for a way to be able to update the comments counter cache
on the post model without changing the lock_version.
This is because I in my edit view of the post also have a control
panel
for managing the comments (ajax). If a comment here is removed
(without
reloading the page and then also the post) the posts counter cache is
updated alongside with its lock_version. If I then try to save the
post
its stale (StaleObjectError).
So I'm looking for a way to be able to update the comments counter
cache
on the post model without changing the lock_version.
I think you're better off fixing this properly than hacking around
with lock_version. If you post the salient parts of the code someone
might be able to offer a suggestion.
OK. So first off suppressing the increment of lock_version would probably meant that your counter cache value would get stomped on (although with partial updates in 2.1 you might get away with it).
the easiest way would be to reload p after having done stuff to comments, or alternatively you might be able to do the stuff you need to do to p before you play with the comments.
Yet another way might be to do stuff to c.post instead of doing them to p.
1. User opens the post edit ui (which contains the control panel for the
comments)
2. User edits the post but doesn't save
3. User destrys one comment through an ajax request
4. User tries to save the edit post
If I where to reload the post the users changes would go lost.