Commit aa6ef51

mo khan <mo@mokhan.ca>
2016-07-04 22:00:55
fix n+1 issues.
1 parent 2eebbd3
Changed files (4)
app
config
environments
app/controllers/concerns/authenticatable.rb
@@ -12,7 +12,9 @@ module Authenticatable
   end
 
   def current_user
-    @current_user ||= current_session.try(:user)
+    @current_user ||= User.find(current_session.try(:user_id))
+  rescue ActiveRecord::RecordNotFound
+    nil
   end
 
   def authenticate!
app/controllers/profiles_controller.rb
@@ -1,12 +1,12 @@
 class ProfilesController < ApplicationController
   def show
-    @user = User.find_by(username: params[:id])
+    @user = User.includes(:workouts, profile: :gym).find_by(username: params[:id])
     @profile = @user.profile
     @program = Program.stronglifts
   end
 
   def edit
-    @profile = current_user.profile
+    @profile = Profile.includes(:user, :gym).find_by(user: current_user)
     @program = Program.stronglifts
   end
 
app/models/user_session.rb
@@ -23,7 +23,7 @@ class UserSession < ApplicationRecord
 
   class << self
     def authenticate(id)
-      active.includes(user: :profile).find_by(id: id)
+      active.find_by(id: id)
     end
   end
 end
config/environments/test.rb
@@ -45,7 +45,7 @@ Rails.application.configure do
   config.after_initialize do
     Bullet.enable = true
     Bullet.bullet_logger = true
-    #Bullet.raise = true
+    Bullet.raise = true
   end
   config.log_level = :fatal
 end