I have an album which has_many photos. A counter_cache setup updates the photos_count column in the album table. How do I limit the number of photos for an album?
ActiveRecord::Base.transaction do
ActiveRecord::Base.connection.execute('LOCK TABLE pictures IN EXCLUSIVE MODE')
if (@album.pictures.count < 10)
@album.pictures.create()
end
end
I believe this is the most correct solution. It guards against concurrency issues/race conditions.