Last active
December 14, 2015 17:49
-
-
Save miio/5124873 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserHighScore < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :high_score_partition | |
attr_accessible :score | |
after_save do | |
partition = HighScorePartition.where("(min < ? AND ? > max)", self.score, self.score).first("LOCK IN SHARE MODE") | |
# Not partition if cheet? | |
raise "Score not range partition" unless partition | |
# Not modify | |
return true partition.id == self.high_score_partition.id | |
# Remove user for old pertition | |
self.partition.decrement! :user_count | |
# Add user for new partition | |
partition.increment! :user_count | |
# New partition | |
self.high_score_partition = partition | |
self.save! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment