{"id":8280,"date":"2022-06-17T05:59:56","date_gmt":"2022-06-16T20:59:56","guid":{"rendered":"https:\/\/yumishin.com\/?p=8280"},"modified":"2022-06-17T05:59:56","modified_gmt":"2022-06-16T20:59:56","slug":"get-system-operation-log","status":"publish","type":"post","link":"https:\/\/yumishin.com\/?p=8280","title":{"rendered":"\u3010rails\u30fbdevise\u3011\u30e6\u30fc\u30b6\u30fc\u306e\u64cd\u4f5c\u5c65\u6b74\u3092\u8868\u793a\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<h2 id=\"i-0\">\u3010rails\u30fbdevise\u3011\u30e6\u30fc\u30b6\u30fc\u306e\u64cd\u4f5c\u5c65\u6b74\u3092\u8868\u793a\u3059\u308b\u65b9\u6cd5<\/h2>\n<h3 id=\"i-1\">\u7d50\u8ad6\u3068\u5b9f\u88c5\u306e\u30a4\u30e1\u30fc\u30b8<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8281\" src=\"https:\/\/yumishin.com\/wp-content\/uploads\/2022\/06\/2022-06-17-5.46.48.png\" alt=\"\" width=\"999\" height=\"634\" srcset=\"https:\/\/yumishin.com\/wp-content\/uploads\/2022\/06\/2022-06-17-5.46.48.png 999w, https:\/\/yumishin.com\/wp-content\/uploads\/2022\/06\/2022-06-17-5.46.48-940x597.png 940w\" sizes=\"auto, (max-width: 999px) 100vw, 999px\"><\/p>\n<ul>\n<li>&nbsp;\u30e6\u30fc\u30b6\u30fc\u306b\u7d10\u3065\u304f\u64cd\u4f5c\u5c65\u6b74\u7528\u306e\u30e2\u30c7\u30eb\u3092\u7528\u610f\u3059\u308b\n<ul>\n<li>\u64cd\u4f5c\u5c65\u6b74\u3092\u6b8b\u3057\u3066\u304a\u304f\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b<\/li>\n<\/ul>\n<\/li>\n<li>\u64cd\u4f5c\u5c65\u6b74\u3092\u6b8b\u3057\u305f\u3044controller\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u306b\u3001\u305d\u308c\u305e\u308c\u64cd\u4f5c\u5c65\u6b74\u3092\u4f5c\u6210\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3092\u4ed5\u8fbc\u3080\n<ul>\n<li>\u4f8b\u3048\u3070users_controller\u306ecreate\u30a2\u30af\u30b7\u30e7\u30f3\u306e\u4e2d\u306b\u3001\u64cd\u4f5c\u5c65\u6b74\u4fdd\u5b58\u7528\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4ed5\u8fbc\u3080<\/li>\n<li>create\u30a2\u30af\u30b7\u30e7\u30f3\u304c\u5b9f\u884c\u3055\u308c\u305f\u3089\u3001\u64cd\u4f5c\u5c65\u6b74\u3082\u4e00\u7dd2\u306b\u4fdd\u5b58\u3055\u308c\u308b<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3 id=\"i-2\">\u5b9f\u88c5\u5185\u5bb9<\/h3>\n<p>\u307e\u305a\u306f\u5c65\u6b74\u3092\u4fdd\u5b58\u3059\u308b\u7528\u306e\u30e2\u30c7\u30eb\u3001\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<div class=\"pre_tag\"><span><i class=\"fa fa-code\"><\/i> terminal<\/span><\/p>\n<pre><code>$ rails g model SystemOperationLog user_id:integer message:string<\/code><\/pre>\n<\/div>\n<p>User\u30e2\u30c7\u30eb\u3068SystemOperationLog\u30e2\u30c7\u30eb\u306f1\u5bfe\u591a\u306e\u95a2\u4fc2\u306b\u306a\u308b\u306e\u3067\u3001\u95a2\u9023\u3065\u3051\u3092\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n<div class=\"pre_tag\"><span><i class=\"fa fa-code\"><\/i> models\/user.rb<\/span><\/p>\n<pre><code>class User &lt; ApplicationRecord\n  has_many :system_operation_logs, dependent: :destroy\nend\n<\/code><\/pre>\n<\/div>\n<div class=\"pre_tag\"><span><i class=\"fa fa-code\"><\/i> models\/system_operation_log.rb<\/span><\/p>\n<pre><code>class SystemOperationLog &lt; ApplicationRecord\n  belongs_to :user, optional: true\nend<\/code><\/pre>\n<\/div>\n<p>\u7d9a\u3044\u3066\u3001\u30e2\u30c7\u30eb\u306b\u5c65\u6b74\u4fdd\u5b58\u7528\u306e\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<div class=\"pre_tag\"><span><i class=\"fa fa-code\"><\/i> models\/system_operation_log.rb<\/span><\/p>\n<pre><code>class SystemOperationLog &lt; ApplicationRecord\n  belongs_to :user, optional: true\n\n  def self.create_log(user_id, message)\n    SystemOperationLog.create(user_id: user_id, message: message)\n  end\nend\n<\/code><\/pre>\n<\/div>\n<p>\u5f8c\u306f\u3001\u5c65\u6b74\u3092\u53d6\u308a\u305f\u3044\u3068\u3053\u308d\u306b\u3001\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u3092\u4ed5\u8fbc\u307f\u307e\u3059\u3002\u4eca\u56de\u306fuser\u306ecreate\u30a2\u30af\u30b7\u30e7\u30f3\u306b\u5165\u308c\u3066\u307f\u307e\u3059\u3002<\/p>\n<div class=\"pre_tag\"><span><i class=\"fa fa-code\"><\/i> controllers\/users_controller.rb<\/span><\/p>\n<pre><code>  def create\n    @user = User.new(user_params)\n\n    respond_to do |format|\n      if @user.save\n        SystemOperationLog.create_log(current_admin.id, \"#{current_admin.name}\u304c#{@user.name}\u3092\u4f5c\u6210\u3057\u307e\u3057\u305f\")\n        format.html { redirect_to @user, notice: \"#{@user.name}\u3092\u4f5c\u6210\u3057\u307e\u3057\u305f\" }\n        format.json { render :show, status: :created, location: @user }\n      else\n        format.html { render :new, status: :unprocessable_entity }\n        format.json { render json: @user.errors, status: :unprocessable_entity }\n      end\n    end\n  end<\/code><\/pre>\n<\/div>\n<p>after_action\u3068\u304b\u3092\u4f7f\u3046\u3068\u3001\u3044\u308d\u3093\u306a\u3068\u3053\u308d\u306b\u4e00\u6c17\u306b\u30ed\u30b0\u3092\u4ed5\u8fbc\u3081\u305d\u3046\u3067\u3059\u304c\u3001\u3069\u3046\u306a\u306e\u304b\u306a\u3068\u304b\u601d\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\u4eca\u56de\u306f\u3001\u4ee5\u4e0a\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3010rails\u30fbdevise\u3011\u30e6\u30fc\u30b6\u30fc\u306e\u64cd\u4f5c\u5c65\u6b74\u3092\u8868\u793a\u3059\u308b\u65b9\u6cd5 \u7d50\u8ad6\u3068\u5b9f\u88c5\u306e\u30a4\u30e1\u30fc\u30b8 &nbsp;\u30e6\u30fc\u30b6\u30fc\u306b\u7d10\u3065\u304f\u64cd\u4f5c\u5c65\u6b74\u7528\u306e\u30e2\u30c7\u30eb\u3092\u7528\u610f\u3059\u308b \u64cd\u4f5c\u5c65\u6b74\u3092\u6b8b\u3057\u3066\u304a\u304f\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b \u64cd\u4f5c\u5c65\u6b74\u3092\u6b8b\u3057\u305f\u3044controller &#8230; <\/p>\n","protected":false},"author":1,"featured_media":8283,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[256],"tags":[],"class_list":{"0":"post-8280","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\/8280","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=8280"}],"version-history":[{"count":2,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts\/8280\/revisions"}],"predecessor-version":[{"id":8284,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts\/8280\/revisions\/8284"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/media\/8283"}],"wp:attachment":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}