Fix user_badges n+1 query.

This commit is contained in:
Vikhyat Korrapati 2014-04-17 21:57:42 +05:30
parent b3ed8b6a32
commit 843023d7c9
3 changed files with 4 additions and 3 deletions

View File

@ -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

View File

@ -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)

View File

@ -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