{"id":3734,"date":"2022-10-20T13:10:50","date_gmt":"2022-10-20T04:10:50","guid":{"rendered":"https:\/\/weseek.co.jp\/tech\/?p=3734"},"modified":"2023-04-17T10:41:27","modified_gmt":"2023-04-17T01:41:27","slug":"rails-%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8b-hash-%e3%81%ae%e3%82%ad%e3%83%bc%e3%81%ae%e6%89%b1%e3%81%84%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6%e3%81%ae-tips","status":"publish","type":"post","link":"https:\/\/weseek.co.jp\/tech\/3734\/","title":{"rendered":"Rails \u306b\u304a\u3051\u308b hash \u306e\u30ad\u30fc\u306e\u6271\u3044\u306b\u3064\u3044\u3066\u306e tips"},"content":{"rendered":"<p>\u307f\u306a\u3055\u3093\u3001\u3053\u3093\u306b\u3061\u306f kota \u3067\u3059\u3002<br \/>\n\u65e5\u9803\u3001\u696d\u52d9\u3067\u306f Rails \u3092\u4f7f\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u5148\u65e5 hash \u306e\u30ad\u30fc\u306e\u6271\u3044\u306b\u3064\u3044\u3066\u3001\u518d\u8a8d\u8b58\u3057\u305f\u3053\u3068\u304c\u3042\u3063\u305f\u306e\u3067\u3001\u7c21\u5358\u306b\u5171\u6709\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p><!--more--><\/p>\n\n<h1>\u56f0\u3063\u305f\u3053\u3068<\/h1>\n<ul>\n<li>model \u304b\u3089\u30c7\u30fc\u30bf\u3092\u6240\u5f97\u3057\u3001\u5909\u6570\u306b\u5165\u308c\u3001\u305d\u306e\u5909\u6570\u306b\u5bfe\u3057 attributes \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066 hash \u5316\u3057\u305f\u3002<\/li>\n<li>hash \u5316\u3057\u305f\u5909\u6570\u304b\u3089 <strong>name<\/strong> \u306e\u5024\u3092\u53d6\u5f97\u3057\u3088\u3046\u3068<code>user[:name]<\/code>\u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3082\u3001nil \u304c\u8fd4\u3063\u3066\u6765\u3066\u53d6\u5f97\u3067\u304d\u306a\u304b\u3063\u305f\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-ruby\">[1] pry(main)&gt; user = User.first\n  User Load (13.8ms)  SELECT &quot;users&quot;.* FROM &quot;users&quot; ORDER BY &quot;users&quot;.&quot;id&quot; ASC LIMIT $1  [[&quot;LIMIT&quot;, 1]]\n=&gt; #&lt;User id: 1, name: &quot;hoge&quot;, created_at: &quot;2022-09-30 08:24:03&quot;, updated_at: &quot;2022-09-30 08:24:03&quot;&gt;\n\n[2] pry(main)&gt; user = user.attributes\n=&gt; {&quot;id&quot;=&gt;1,\n &quot;name&quot;=&gt;&quot;hoge&quot;,\n &quot;created_at&quot;=&gt;Fri, 30 Sep 2022 17:24:03 JST +09:00,\n &quot;updated_at&quot;=&gt;Fri, 30 Sep 2022 17:24:03 JST +09:00}\n\n[2] pry(main)&gt; user[:name]\n=&gt; nil<\/code><\/pre>\n<h1>\u8abf\u67fb<\/h1>\n<p>\u5148\u8ff0\u3057\u305f\u901a\u308a model \u304b\u3089\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u3066\u3001attributes \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u305f\u969b\u306e\u8fd4\u308a\u5024\u306f\u4e0b\u8a18\u306e\u901a\u308a\u3067\u3057\u305f\u3002<br \/>\n\u3053\u3053\u3067\u3001\u30ad\u30fc\u304c\u6587\u5b57\u5217\u306b\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u306b\u6c17\u4ed8\u304d\u307e\u3057\u305f\u3002<\/p>\n<pre><code class=\"language-ruby\">[2] pry(main)&gt; user = user.attributes\n=&gt; {&quot;id&quot;=&gt;1,\n &quot;name&quot;=&gt;&quot;hoge&quot;,\n &quot;created_at&quot;=&gt;Fri, 30 Sep 2022 17:24:03 JST +09:00,\n &quot;updated_at&quot;=&gt;Fri, 30 Sep 2022 17:24:03 JST +09:00}<\/code><\/pre>\n<p>\u5f53\u305f\u308a\u524d\u3067\u3059\u304c\u3001\u5024\u3092\u53d6\u5f97\u3059\u308b\u306b\u306f\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u5f62\u5f0f\u3068\u540c\u3058\u5f62\u5f0f\u3067\u30a2\u30af\u30bb\u30b9\u3057\u306a\u3044\u3068\u53d6\u5f97\u3067\u304d\u306a\u3044\u3053\u3068\u3092\u6539\u3081\u3066\u8a8d\u8b58\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre><code class=\"language-ruby\">[4] pry(main)&gt; user[&quot;name&quot;]\n=&gt; &quot;hoge&quot;<\/code><\/pre>\n<p>params \u306e\u5024\u3092\u53d6\u5f97\u3059\u308b\u969b\u306a\u3069\u306b\u30b7\u30f3\u30dc\u30eb\u3092\u4f7f\u3046\u3053\u3068\u304c\u591a\u304b\u3063\u305f\u70ba\u3001\u5c11\u3005\u30cf\u30de\u3063\u3066\u3057\u307e\u3063\u305f\u3068\u3044\u3046\u304a\u8a71\u3067\u3057\u305f\u3002<\/p>\n<h1>\u8ffd\u52a0\u8abf\u67fb\u30fb\u30e1\u30bd\u30c3\u30c9\u307e\u3068\u3081<\/h1>\n<p>\u3044\u3044\u6a5f\u4f1a\u306a\u306e\u3067 hash \u306b\u3064\u3044\u3066\u3082\u3046\u5c11\u3057\u8abf\u3079\u3001\u4f7f\u3048\u305d\u3046\u306a\u30e1\u30bd\u30c3\u30c9\u3092\u307e\u3068\u3081\u307e\u3057\u305f\u3002<\/p>\n<h3>\u30ad\u30fc\u3092\u6587\u5b57\u5217\u304b\u3089\u30b7\u30f3\u30dc\u30eb\u306b\u5909\u63db<\/h3>\n<ul>\n<li>symbolize_keys\n<ul>\n<li>Rails \u304c\u7528\u610f\u3057\u3066\u3044\u308b\u30e1\u30bd\u30c3\u30c9\u3002\u5185\u90e8\u7684\u306b\u306f\u3001ruby \u306e <code>transform_keys(&amp;:to_sym)<\/code> \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u3002<\/li>\n<li>\u53c2\u8003\n<ul>\n<li><a href=\"https:\/\/api.rubyonrails.org\/classes\/Hash.html#method-i-symbolize_keys\">https:\/\/api.rubyonrails.org\/classes\/Hash.html#method-i-symbolize_keys<\/a><\/li>\n<li><a href=\"https:\/\/docs.ruby-lang.org\/ja\/latest\/method\/Hash\/i\/transform_keys.html\">https:\/\/docs.ruby-lang.org\/ja\/latest\/method\/Hash\/i\/transform_keys.html<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"language-ruby\">[\uff11] pry(main)&gt; user.symbolize_keys\n=&gt; {:id=&gt;1,\n :name=&gt;&quot;hoge&quot;,\n :created_at=&gt;Fri, 30 Sep 2022 17:24:03 JST +09:00,\n :updated_at=&gt;Fri, 30 Sep 2022 17:24:03 JST +09:00}<\/code><\/pre>\n<h3>\u30ad\u30fc\u3092\u30b7\u30f3\u30dc\u30eb\u304b\u3089\u6587\u5b57\u5217\u306b\u5909\u63db<\/h3>\n<ul>\n<li>stringify_keys\n<ul>\n<li>Rails \u304c\u7528\u610f\u3057\u3066\u3044\u308b\u30e1\u30bd\u30c3\u30c9\u3002\u5185\u90e8\u7684\u306b\u306f\u3001ruby \u306e <code>transform_keys(&amp;:to_s)<\/code> \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u3002<\/li>\n<li>\u53c2\u8003: <a href=\"https:\/\/api.rubyonrails.org\/classes\/Hash.html#method-i-stringify_keys\">https:\/\/api.rubyonrails.org\/classes\/Hash.html#method-i-stringify_keys<\/a>\n<pre><code class=\"language-ruby\">[1] pry(main)&gt; user.stringify_keys\n=&gt; {&quot;id&quot;=&gt;1,\n&quot;name&quot;=&gt;&quot;admin&quot;,\n&quot;created_at&quot;=&gt;Fri, 30 Sep 2022 17:24:03 JST +09:00,\n&quot;updated_at&quot;=&gt;Fri, 30 Sep 2022 17:24:03 JST +09:00}<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>hash \u306e\u30cd\u30b9\u30c8\u3082\u5909\u63db\u3057\u305f\u3044<\/h3>\n<p>\u4e0a\u8a18\uff12\u3064\u306e\u30e1\u30bd\u30c3\u30c9\u306e\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001hash \u5185\u306b\u30cd\u30b9\u30c8\u304c\u3042\u308b\u3068\u3001\u305d\u306e\u30cd\u30b9\u30c8\u306f\u5909\u63db\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n<pre><code class=\"language-ruby\">[1] pry(main)&gt; user\n=&gt; {&quot;id&quot;=&gt;1,\n &quot;name&quot;=&gt;&quot;hoge&quot;,\n &quot;created_at&quot;=&gt;Fri, 30 Sep 2022 17:24:03 JST +09:00,\n &quot;updated_at&quot;=&gt;Fri, 30 Sep 2022 17:24:03 JST +09:00,\n &quot;group&quot;=&gt;{&quot;id&quot;=&gt;1, &quot;group_name&quot;=&gt;&quot;fuga&quot;}}\n\n[2] pry(main)&gt; user.symbolize_keys\n=&gt; {:id=&gt;1,\n :name=&gt;&quot;hoge&quot;,\n :created_at=&gt;Fri, 30 Sep 2022 17:24:03 JST +09:00,\n :updated_at=&gt;Fri, 30 Sep 2022 17:24:03 JST +09:00,\n :group=&gt;{&quot;id&quot;=&gt;1, &quot;group_name&quot;=&gt;&quot;fuga&quot;}} &lt;- \u30b7\u30f3\u30dc\u30eb\u306b\u5909\u63db\u3055\u308c\u3066\u306a\u3044<\/code><\/pre>\n<p>\u30cd\u30b9\u30c8\u3082\u5909\u63db\u3057\u305f\u3044\u5834\u5408\u306f\u3001<code>deep_symbolize_keys (deep_stringify_keys)<\/code> \u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u5909\u63db\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre><code class=\"language-ruby\">[3] pry(main)> user.deep_symbolize_keys\n=> {:id=>1,\n :name=>\"hoge\",\n :created_at=>Fri, 30 Sep 2022 17:24:03 JST +09:00,\n :updated_at=>Fri, 30 Sep 2022 17:24:03 JST +09:00,\n :group=>{:id=>1, :group_name=>\"fuga\"}}<\/code><\/pre>\n<p>\u203b ruby \u306e\u30e1\u30bd\u30c3\u30c9 <code>transform_keys<\/code> \u3082\u540c\u69d8\u306b <code>deep_transform_keys<\/code> \u3067\u3001\u30cd\u30b9\u30c8\u306e\u5909\u63db\u3082\u53ef\u80fd<\/p>\n<h3>\u6587\u5b57\u5217\u3067\u3082\u30b7\u30f3\u30dc\u30eb\u3067\u3082\u5024\u3092\u53d6\u5f97\u3057\u305f\u3044<\/h3>\n<ul>\n<li>with_indifferent_access<\/li>\n<\/ul>\n<p>\u5148\u307b\u3069\u307e\u3067\u4f7f\u3063\u3066\u3044\u305f\u5909\u6570 <code>user<\/code> \u306e\u3000class \u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-ruby\">[\uff11] pry(main)&gt; user.class\n=&gt; Hash<\/code><\/pre>\n<p>\u3053\u3053\u3067\u3001<code>with_indifferent_access<\/code> \u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3044 class \u3092 <code>ActiveSupport::HashWithIndifferentAccess<\/code> \u306b\u5909\u66f4\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u53c2\u8003\n<ul>\n<li><a href=\"https:\/\/api.rubyonrails.org\/classes\/ActiveSupport\/HashWithIndifferentAccess.html\">https:\/\/api.rubyonrails.org\/classes\/ActiveSupport\/HashWithIndifferentAccess.html<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"language-ruby\">[1] pry(main)&gt; user = user.with_indifferent_access\n\n...\n\n[2] pry(main)&gt; user.class\n=&gt; ActiveSupport::HashWithIndifferentAccess<\/code><\/pre>\n<p>\u6587\u5b57\u5217\u3068\u30b7\u30f3\u30dc\u30eb\u306e\u4e21\u65b9\u3067\u5024\u3092\u53d6\u5f97\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-ruby\">[3] pry(main)&gt; user[:name]\n=&gt; &quot;hoge&quot;\n[4] pry(main)&gt; user[&quot;name&quot;]\n=&gt; &quot;hoge&quot;<\/code><\/pre>\n<p>\u6587\u5b57\u5217\u3001\u30b7\u30f3\u30dc\u30eb\u306e\u4e21\u65b9\u3067\u5024\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3057\u305f\u3002<br \/>\nparams \u3082\u3053\u306e <code>ActiveSupport::HashWithIndifferentAccess<\/code> class \u3092\u7d99\u627f\u3057\u3066\u3044\u3066\u3001\u6587\u5b57\u5217\u3001\u30b7\u30f3\u30dc\u30eb\u306e\u4e21\u65b9\u3067\u5024\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n<h1>\u6700\u5f8c\u306b<\/h1>\n<p>\u3044\u304b\u304c\u3060\u3063\u305f\u3067\u3057\u3087\u3046\u304b\uff1f<br \/>\n\u4eca\u56de\u306f\u3001Rails \u3067\u306e hash \u306e\u30ad\u30fc\u306e\u6271\u3044\u3084\u95a2\u9023\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u3001\u7c21\u5358\u306b\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\nhash \u306b\u95a2\u308f\u308b\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u306f\u591a\u3005\u3042\u308a\u307e\u3059\u306e\u3067\u3001\u3054\u81ea\u8eab\u3067\u3082\u8abf\u3079\u3066\u307f\u3066\u4e0b\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u307f\u306a\u3055\u3093\u3001\u3053\u3093\u306b\u3061\u306f kota \u3067\u3059\u3002 \u65e5\u9803\u3001\u696d\u52d9\u3067\u306f Rails \u3092\u4f7f\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u5148\u65e5 hash \u306e\u30ad\u30fc\u306e\u6271\u3044\u306b\u3064\u3044\u3066\u3001\u518d\u8a8d\u8b58\u3057\u305f\u3053\u3068\u304c\u3042\u3063\u305f\u306e\u3067\u3001\u7c21\u5358\u306b\u5171\u6709\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"author":3,"featured_media":3756,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[154],"tags":[],"_links":{"self":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/3734"}],"collection":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/comments?post=3734"}],"version-history":[{"count":21,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/3734\/revisions"}],"predecessor-version":[{"id":4005,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/3734\/revisions\/4005"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media\/3756"}],"wp:attachment":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media?parent=3734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/categories?post=3734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/tags?post=3734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}