Skip to content

Instantly share code, notes, and snippets.

@miio
Last active December 14, 2015 17:49
Show Gist options
  • Save miio/5124873 to your computer and use it in GitHub Desktop.
Save miio/5124873 to your computer and use it in GitHub Desktop.
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