Fix user_badges n+1 query.
This commit is contained in:
parent
b3ed8b6a32
commit
843023d7c9
|
@ -6,8 +6,9 @@ class UserBadgesController < ApplicationController
|
|||
user_badges = user.user_badges
|
||||
else
|
||||
badge = fetch_badge_from_params
|
||||
user_badges = badge.user_badges.order('granted_at DESC').limit(200).to_a
|
||||
user_badges = badge.user_badges.order('granted_at DESC').limit(200)
|
||||
end
|
||||
user_badges = user_badges.includes(:user, :granted_by, badge: :badge_type)
|
||||
render_serialized(user_badges, UserBadgeSerializer, root: "user_badges")
|
||||
end
|
||||
|
||||
|
|
|
@ -497,7 +497,7 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def featured_user_badges
|
||||
user_badges.joins(:badge).order('badges.badge_type_id ASC, badges.grant_count ASC').includes(:granted_by, badge: :badge_type).limit(3)
|
||||
user_badges.joins(:badge).order('badges.badge_type_id ASC, badges.grant_count ASC').includes(:user, :granted_by, badge: :badge_type).limit(3)
|
||||
end
|
||||
|
||||
def self.count_by_signup_date(sinceDaysAgo=30)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
class UserBadgeSerializer < ApplicationSerializer
|
||||
attributes :id, :granted_at
|
||||
|
||||
has_one :user
|
||||
has_one :badge
|
||||
has_one :user, serializer: BasicUserSerializer, root: :users
|
||||
has_one :granted_by, serializer: BasicUserSerializer, root: :users
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue