質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1340閲覧

rails tutorial のカレントユーザーのキャッシュについて

shumbow

総合スコア35

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/11/03 09:37

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

@にしてください。
$にすると誰がloginしても最初にloginした人のアカウントのままになってしまいます

>一度current_userすればメモリに保存してその後は再度クエリを発行しないということだったと思うのですが、
そのとおりです、ですがその有効範囲・期間は そのリクエスト完了までです。

>ログを見てみるとnewやindexにいくたびにクエリを発行しています。
これも正しい動作です。

@のスコープはそのインスタンスです。
GETやPOSTのたびにControllerの新しいインスタンスが作られるので、その都度クエリが必要です

投稿2019/11/03 09:44

winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shumbow

2019/11/03 23:16

ありがとうございます!またお願いします!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問