rails tutorial のセッションヘルパーでおそらく一番重要なcurrent_user
rb
1 def current_user 2 if (user_id = session[:user_id]) 3 @current_user ||= User.find_by(id: user_id) 4 elsif (user_id = cookies.signed[:user_id]) 5 user = User.find_by(id: user_id) 6 if user && user.authenticated?(:remember, cookies[:remember_token]) 7 log_in user 8 @current_user = user 9 end 10 end 11 end
で@current_user ||= .......
のように書くと一度current_userすればメモリに保存してその後は再度クエリを発行しないということだったと思うのですが、ログを見てみるとnewやindexにいくたびにクエリを発行しています。
Started GET "/users/new" for ::1 at 2019-11-03 18:23:11 +0900 Processing by UsersController#new as HTML Rendering users/new.html.erb within layouts/application Rendered shared/_error_messages.html.erb (Duration: 0.2ms | Allocations: 21) Rendered users/_form.html.erb (Duration: 30.8ms | Allocations: 927) Rendered users/new.html.erb within layouts/application (Duration: 33.5ms | Allocations: 1310) User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", "ca89455c-a092-497f-b3dd-9744f6dd852f"], ["LIMIT", 1]]⬅️ここ ↳ app/helpers/sessions_helper.rb:8:in `current_user' Rendered layouts/_deviation_common.html.erb (Duration: 1.9ms | Allocations: 728) Rendered layouts/_deviation_footer.html.erb (Duration: 0.2ms | Allocations: 71) Completed 200 OK in 59ms (Views: 58.1ms | ActiveRecord: 0.3ms | Allocations: 5489) Started GET "/users" for ::1 at 2019-11-03 18:23:12 +0900 Processing by UsersController#index as HTML **User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", "ca89455c-a092-497f-b3dd-9744f6dd852f"], ["LIMIT", 1]]**⬅️ここ ↳ app/helpers/sessions_helper.rb:8:in `current_user' Rendering users/index.html.erb within layouts/application User Load (41.7ms) SELECT "users".* FROM "users" ORDER BY "users"."created_at" DESC ↳ app/views/users/index.html.erb:3 Rendered collection of users/_user.html.erb [2 times] (Duration: 1.5ms | Allocations: 357) Rendered users/index.html.erb within layouts/application (Duration: 46.0ms | Allocations: 1433) Rendered layouts/_deviation_common.html.erb (Duration: 0.3ms | Allocations: 116) Rendered layouts/_deviation_footer.html.erb (Duration: 0.1ms | Allocations: 71) Completed 200 OK in 70ms (Views: 25.3ms | ActiveRecord: 42.3ms | Allocations: 5583)
スコープが有効でないのかなと思ったのでグローバル変数にしてみたところちゃんと最初の一回だけになりました。
rb
1 def current_user 2 if (user_id = session[:user_id]) 3 $current_user ||= User.find_by(id: user_id) ⬅️ここ 4 elsif (user_id = cookies.signed[:user_id]) 5 user = User.find_by(id: user_id) 6 if user && user.authenticated?(:remember, cookies[:remember_token]) 7 log_in user 8 $current_user = user 9 end 10 end 11 end
Started GET "/users/new" for ::1 at 2019-11-03 18:34:21 +0900 Processing by UsersController#new as HTML Rendering users/new.html.erb within layouts/application Rendered shared/_error_messages.html.erb (Duration: 0.1ms | Allocations: 21) Rendered users/_form.html.erb (Duration: 1.0ms | Allocations: 557) Rendered users/new.html.erb within layouts/application (Duration: 1.3ms | Allocations: 663) Rendered layouts/_deviation_common.html.erb (Duration: 0.4ms | Allocations: 107) Rendered layouts/_deviation_footer.html.erb (Duration: 0.2ms | Allocations: 68) Completed 200 OK in 17ms (Views: 16.0ms | ActiveRecord: 0.0ms | Allocations: 4067) Started GET "/users" for ::1 at 2019-11-03 18:34:56 +0900 Processing by UsersController#index as HTML Rendering users/index.html.erb within layouts/application User Load (0.5ms) SELECT "users".* FROM "users" ORDER BY "users"."created_at" DESC ↳ app/views/users/index.html.erb:3 Rendered collection of users/_user.html.erb [2 times] (Duration: 0.8ms | Allocations: 208) Rendered users/index.html.erb within layouts/application (Duration: 3.6ms | Allocations: 1012) Rendered layouts/_deviation_common.html.erb (Duration: 0.4ms | Allocations: 108) Rendered layouts/_deviation_footer.html.erb (Duration: 0.2ms | Allocations: 68) Completed 200 OK in 23ms (Views: 20.8ms | ActiveRecord: 0.5ms | Allocations: 4450)
@でなくて$を使った方が正解なのでしょうか?
なにか勘違いしているところあったら教えてください。
rails6.0.0 ruby 2.6.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/11/03 23:16