diff --git a/app/models/favorite.rb b/app/models/favorite.rb index 06bd08bb8..c0ff859a2 100644 --- a/app/models/favorite.rb +++ b/app/models/favorite.rb @@ -14,10 +14,13 @@ class Favorite < ActiveRecord::Base def self.add(post, user) Favorite.transaction do + User.where(:id => user.id).select("id").lock("FOR UPDATE NOWAIT").first + return if Favorite.for_user(user.id).where(:user_id => user.id, :post_id => post.id).exists? Favorite.create!(:user_id => user.id, :post_id => post.id) - Post.where(:id => post.id).update_all("fav_count = fav_count + 1") - Post.where(:id => post.id).update_all("score = score + 1") if user.is_gold? + updates = "fav_count = fav_count + 1" + updates = "#{updates}, score = score + 1" if user.is_gold? + Post.where(:id => post.id).update_all(updates) post.append_user_to_fav_string(user.id) User.where(:id => user.id).update_all("favorite_count = favorite_count + 1") user.favorite_count += 1 @@ -28,10 +31,13 @@ class Favorite < ActiveRecord::Base def self.remove(post, user) Favorite.transaction do + User.where(:id => user.id).select("id").lock("FOR UPDATE NOWAIT").first + return unless Favorite.for_user(user.id).where(:user_id => user.id, :post_id => post.id).exists? Favorite.destroy_all(:user_id => user.id, :post_id => post.id) - Post.where(:id => post.id).update_all("fav_count = fav_count - 1") - Post.where(:id => post.id).update_all("Score = score - 1") if user.is_gold? + updates = "fav_count = fav_count - 1" + updates = "#{updates}, score = score - 1" if user.is_gold? + Post.where(:id => post.id).update_all(updates) post.delete_user_from_fav_string(user.id) User.where(:id => user.id).update_all("favorite_count = favorite_count - 1") user.favorite_count -= 1 diff --git a/db/structure.sql b/db/structure.sql index 2b788641c..6ebf87b7f 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -3,7 +3,6 @@ -- SET statement_timeout = 0; -SET lock_timeout = 0; SET client_encoding = 'UTF8'; SET standard_conforming_strings = on; SET check_function_bodies = false;