Skip to content

Commit

Permalink
Merge pull request #380 from turingschool-projects/speed_up_calls_for…
Browse files Browse the repository at this point in the history
…_user

Remove n+1 queries with includes
  • Loading branch information
s-espinosa authored Sep 11, 2018
2 parents 31a4088 + bc2885c commit 5a225b2
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/users/by_cohort_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Api::V1::Users::ByCohortController < Api::V1::ApiController
before_action :doorkeeper_authorize!

def index
users = User.where(cohort_id: params[:cohort_id])
users = User.with_serializer_info.where(cohort_id: params[:cohort_id])

render json: users, each_serializer: UserSerializer, root_url: root_url, status: 200
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/users/by_github_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Api::V1::Users::ByGithubController < Api::V1::ApiController
before_action :doorkeeper_authorize!

def show
if params[:q] && user = User.find_by(git_hub: params[:q])
if params[:q] && user = User.with_serializer_info.find_by(git_hub: params[:q])
render json: user, serializer: UserSerializer, status: 200
else
render json: { error: "User not found" }, status: 404
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/users/by_name_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Api::V1::Users::ByNameController < Api::V1::ApiController
before_action :doorkeeper_authorize!

def index
users = User.search_by_name(params[:q])
users = User.with_serializer_info.search_by_name(params[:q])

render json: users, each_serializer: UserSerializer, root_url: root_url, status: 200
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def index
end

def show
@user = User.find(params[:id])
@user = User.with_serializer_info.find(params[:id])
render json: @user, serializer: SingleUserSerializer, root_url: root_url, status: 200
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def has_role?(role)
roles.any? { |user_role| user_role.name == role }
end

def self.with_serializer_info
includes(:groups, :roles)
end

def self.search_by_name(term)
User.where(
"upper(first_name) LIKE ? OR
Expand Down
4 changes: 2 additions & 2 deletions app/serializers/single_user_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def cohort
end

def groups
object.groups.map {|group| group.name}
object.groups.pluck(:name)
end

def roles
object.roles.map {|role| role.name}
object.roles.pluck(:name)
end

end
4 changes: 1 addition & 3 deletions app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def image_url
end

def groups
object.groups.map do |group|
group.name
end
object.groups.pluck(:name)
end

def cohort
Expand Down

0 comments on commit 5a225b2

Please sign in to comment.