Marcin Simonides wrote:
def increment_view_count! Photo.transaction do lock! # optimistic lock must by turned off for this update increment(:views) update_without_lock end end
If you don't need to read the current view count when incrementing, an alternative is:
def increment_view_count! Photo.increment_counter(:views, id) end