{"id":8295,"date":"2022-06-21T06:00:32","date_gmt":"2022-06-20T21:00:32","guid":{"rendered":"https:\/\/yumishin.com\/?p=8295"},"modified":"2022-06-21T06:00:32","modified_gmt":"2022-06-20T21:00:32","slug":"rails-current-user","status":"publish","type":"post","link":"https:\/\/yumishin.com\/?p=8295","title":{"rendered":"\u3010Rails\u3011current_user\u3092model\u3067\u4f7f\u7528\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<h2 id=\"i-0\">\u3010Rails\u3011current_user\u3092model\u3067\u4f7f\u7528\u3059\u308b\u65b9\u6cd5<\/h2>\n<h3 id=\"i-1\">\u7d50\u8ad6\uff08\u5b9f\u88c5\u306e\u624b\u9806\uff09<\/h3>\n<p>\u307e\u305a\u5b9f\u88c5\u306e\u624b\u9806\u3092\u304a\u4f1d\u3048\u3057\u307e\u3059\u3002\u7c21\u5358\u306b\u3044\u3046\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n<ol>\n<li>user.rb\u306b\u3066\u3001Thread.current\u306bcurrent_user\u3092\u30bb\u30c3\u30c8\u3059\u308b\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u7528\u610f\u3059\u308b<\/li>\n<li>user.rb\u306b\u7528\u610f\u3057\u305f\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3092application_controller.rb\u306b\u3066\u4f7f\u7528\u3057\u3066\u3001Thread.current\u306bcurrent_user\u3092\u30bb\u30c3\u30c8\u3059\u308b<\/li>\n<li>user.rb\u306e\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3001current_user\u3092\u547c\u3073\u3060\u3059<\/li>\n<\/ol>\n<p>\u203b\u305d\u3082\u305d\u3082current_user\u306f\u5177\u4f53\u7684\u306a\u3082\u306e\u3067\u3042\u308a\u3001\u57fa\u672c\u7684\u306b\u306f\u62bd\u8c61\u5316\u3055\u308c\u305f\u30e2\u30c7\u30eb\u3067\u547c\u3073\u51fa\u3059\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u672c\u5f53\u306b\u5fc5\u8981\u304b\u3069\u3046\u304b\u5224\u65ad\u3057\u3066\u3001\u5b9f\u88c5\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h3 id=\"i-2\">\u5b9f\u88c5\u306b\u3064\u3044\u3066<\/h3>\n<p>\u307e\u305a\u306fuser.rb\u306b\u3001\u4ee5\u4e0b\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n<div class=\"pre_tag\"><span><i class=\"fa fa-code\"><\/i> user.rb<\/span><\/p>\n<pre><code>\nclass User &lt; ApplicationRecord\n  def self.current_user=(user)\n    Thread.current[:user] = user # \u73fe\u5728\u306e\u30b9\u30ec\u30c3\u30c9\u306buser\u3092\u8a2d\u5b9a\u3059\u308b\u30e1\u30bd\u30c3\u30c9\n  end\n\n  def self.current_user\n    Thread.current[:user] # \u73fe\u5728\u306e\u30b9\u30ec\u30c3\u30c9\u306b\u767b\u9332\u3055\u308c\u3066\u3044\u308b\u30e6\u30fc\u30b6\u30fc\u3092\u547c\u3073\u51fa\u3059\n  end\nend\n<\/code><\/pre>\n<\/div>\n<p>\u7d9a\u3044\u3066\u3001application_controller.rb\u306b\u4ee5\u4e0b\u306e\u8a18\u8ff0\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n<div class=\"pre_tag\"><span><i class=\"fa fa-code\"><\/i> application_controller.rb<\/span><\/p>\n<pre><code>class ApplicationController &lt; ActionController::Base\n  before_action :set_current_user # before\u30a2\u30af\u30b7\u30e7\u30f3\u3067\u3001user\u3092\u6bce\u56de\u30bb\u30c3\u30c8\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\n\n  def set_current_user\n    User.current_user = current_user # \u5148\u7a0b\u5b9a\u7fa9\u3057\u305f\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u3001\u305d\u3061\u3089\u306b\u73fe\u5728\u306ecurrent_user\u3092\u4ee3\u5165\n  end\nend<\/code><\/pre>\n<\/div>\n<p>\u6700\u5f8c\u306b\u3001model\u3067\u547c\u3073\u51fa\u3059\u3068\u304d\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u547c\u3073\u51fa\u3057\u307e\u3059\u3002<\/p>\n<div class=\"pre_tag\"><span><i class=\"fa fa-code\"><\/i> \u30b3\u30fc\u30c9<\/span><\/p>\n<pre><code>User.current_user # \u3053\u308c\u3067current_user\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u308b<\/code><\/pre>\n<\/div>\n<p>\u4ee5\u4e0a\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3010Rails\u3011current_user\u3092model\u3067\u4f7f\u7528\u3059\u308b\u65b9\u6cd5 \u7d50\u8ad6\uff08\u5b9f\u88c5\u306e\u624b\u9806\uff09 \u307e\u305a\u5b9f\u88c5\u306e\u624b\u9806\u3092\u304a\u4f1d\u3048\u3057\u307e\u3059\u3002\u7c21\u5358\u306b\u3044\u3046\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3057\u307e\u3059\u3002 user.rb\u306b\u3066\u3001Thread.current\u306bcurrent_ &#8230; <\/p>\n","protected":false},"author":1,"featured_media":8296,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[256],"tags":[],"class_list":{"0":"post-8295","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-it","8":"entry"},"_links":{"self":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts\/8295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8295"}],"version-history":[{"count":1,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts\/8295\/revisions"}],"predecessor-version":[{"id":8297,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts\/8295\/revisions\/8297"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/media\/8296"}],"wp:attachment":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}