group member count optimization

This commit is contained in:
2458773093
2019-07-22 03:46:00 +03:00
parent bd3f453c0d
commit bf2d54201b
7 changed files with 45 additions and 5 deletions

View File

@@ -15,6 +15,7 @@
# is_archived :boolean default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
# member_count :integer default(0)
#
class Group < ApplicationRecord

View File

@@ -21,10 +21,20 @@ class GroupAccount < ApplicationRecord
validates :account_id, uniqueness: { scope: :group_id }
after_commit :remove_relationship_cache
after_create :increment_member_count
after_destroy :decrement_member_count
private
def remove_relationship_cache
Rails.cache.delete("relationship:#{account_id}:group#{group_id}")
end
def increment_member_count
group&.increment!(:member_count)
end
def decrement_member_count
group&.decrement!(:member_count)
end
end

View File

@@ -22,8 +22,4 @@ class REST::GroupSerializer < ActiveModel::Serializer
full_asset_url(object.cover_image.url)
end
def member_count
object.accounts.count
end
end