{"id":1068,"date":"2012-12-13T00:12:48","date_gmt":"2012-12-12T15:12:48","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=1068"},"modified":"2018-11-14T16:33:58","modified_gmt":"2018-11-14T07:33:58","slug":"rails%e3%81%a7%e5%a4%89%e6%9b%b4%e5%89%8d%e3%81%ae%e5%80%a4%e3%80%81%e5%9e%8b%e5%a4%89%e6%8f%9b%e5%89%8d%e3%81%ae%e5%80%a4%e3%80%81%e3%81%aa%e3%81%a9%e3%81%aa%e3%81%a9","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2012\/12\/13\/rails%e3%81%a7%e5%a4%89%e6%9b%b4%e5%89%8d%e3%81%ae%e5%80%a4%e3%80%81%e5%9e%8b%e5%a4%89%e6%8f%9b%e5%89%8d%e3%81%ae%e5%80%a4%e3%80%81%e3%81%aa%e3%81%a9%e3%81%aa%e3%81%a9\/","title":{"rendered":"Rails\u3067\u5909\u66f4\u524d\u306e\u5024\u3001\u578b\u5909\u63db\u524d\u306e\u5024\u3001\u306a\u3069\u306a\u3069"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>ActiveRecord \u306f\u4f7f\u3044\u52dd\u624b\u306e\u826f\u3044\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/p>\n<p>ActiveRecord::Base \u30af\u30e9\u30b9\u3092\u7d99\u627f\u3059\u308b\u3060\u3051\u3067\u3001\u307b\u3068\u3093\u3069\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u306a\u304f\u3001\u305d\u306e\u6069\u6075\u306b\u9810\u304b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001\u305d\u306e\u3042\u308a\u304c\u305f\u3044\u6069\u6075\u306e\u4e2d\u304b\u3089\u3001\u5c5e\u6027\u306b\u4ee3\u5165\u3057\u305f\u5024\u306b\u95a2\u3059\u308b\u90e8\u5206\u3092\u30d4\u30c3\u30af\u30a2\u30c3\u30d7\u3057\u307e\u3059\u3002<\/p>\n<p>\u5177\u4f53\u7684\u306b\u306f\u3001\u578b\u5909\u63db\u524d\u306e\u5024\u304c\u53d6\u5f97\u3067\u304d\u308b\u3053\u3068\u3001\u5c5e\u6027\u306e\u5909\u66f4\u524d\u306e\u5024\u3092\u77e5\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3053\u3068\u306a\u3069\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u578b\u5909\u63db\u524d\u306e\u5024<\/h2>\n<p>\u4f8b\u3068\u3057\u3066 User \u3068\u3044\u3046\u30e2\u30c7\u30eb\u304c\u3042\u308a\u3001\u5e74\u9f62\u3092\u6570\u5024\u578b\u3067\u4fdd\u6301\u3059\u308b age \u3068\u3044\u3046\u5c5e\u6027\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<p>Rails \u306e ActiveRecord \u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u304a\u3051\u308b\u30ab\u30e9\u30e0\u306e\u578b\u306b\u5fdc\u3058\u3066\u3001\u4ee3\u5165\u3055\u308c\u305f\u5024\u3092\u578b\u5909\u63db\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u6570\u5024\u578b\u306e age \u306b \"123\" \u3068\u3044\u3046\u6587\u5b57\u5217\u3092\u4ee3\u5165\u3057\u305f\u3068\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">user = User.new\r\nuser.age = \"123\"<\/pre>\n<p>\u4ee3\u5165\u5f8c\u306b user.age \u3092\u53c2\u7167\u3059\u308b\u3068\u3001\u6570\u5024\u306b\u5909\u63db\u3055\u308c\u305f\u5024\u304c\u8fd4\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">user.age # =&gt; 123<\/pre>\n<p>\u3057\u304b\u3057\u3001\u578b\u5909\u63db\u3055\u308c\u308b\u524d\u306e\u5024\u3092\u77e5\u308b\u5fc5\u8981\u304c\u3042\u308b\u30b1\u30fc\u30b9\u304c\u5c11\u306a\u3044\u306a\u304c\u3089\u3082\u5b58\u5728\u3057\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u51e6\u7406\u3067\u578b\u5909\u63db\u524d\u306e\u5024\u306b\u5bfe\u3057\u3066\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3092\u30c1\u30a7\u30c3\u30af\u3057\u305f\u3044\u5834\u5408\u306a\u3069\u3067\u3059\u3002<\/p>\n<p>\u305d\u306e\u3088\u3046\u306a\u5834\u5408\u306f\u3001\u300c\u5c5e\u6027\u540d_before_type_cast\u300d\u3067\u578b\u5909\u63db\u524d\u306e\u5024\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">user.age_before_type_cast # =&gt; \"123\"<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u5909\u66f4\u3055\u308c\u305f\u5c5e\u6027<\/h2>\n<p>\u5909\u66f4\u3055\u308c\u305f\u5c5e\u6027\u3092\u77e5\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3068\u3057\u3066\u3001ID=1 \u306e\u30e6\u30fc\u30b6\u3092 find \u3057\u305f\u3089\u30e6\u30fc\u30b6\u540d\u304c\u300c\u305f\u308d\u3046\u300d\u3060\u3063\u305f\u3001\u3068\u3044\u3046\u3068\u3053\u308d\u304b\u3089\u59cb\u3081\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">user = User.find(1)\r\nuser.name # =&gt; \"\u305f\u308d\u3046\"<\/pre>\n<p>\u540d\u524d\u3092\u300c\u306f\u306a\u3053\u300d\u306b\u5909\u66f4\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">user.name = \"\u306f\u306a\u3053\"<\/pre>\n<p>\u540d\u524d\u3092\u5909\u66f4\uff08\u65b0\u3057\u3044\u5024\u3092\u4ee3\u5165\uff09\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001user \u306f\u5909\u66f4\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5909\u66f4\u3055\u308c\u305f\u304b\u3069\u3046\u304b\u306f\u3001changed? \u30e1\u30bd\u30c3\u30c9\u3067\u77e5\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">user.changed? # =&gt; true<\/pre>\n<p>\u5c5e\u6027\u3054\u3068\u306b\u5909\u66f4\u3055\u308c\u305f\u304b\u3069\u3046\u304b\u3092\u77e5\u308a\u305f\u3044\u5834\u5408\u306f\u300c\u5c5e\u6027\u540d_changed?\u300d\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">user.name_changed? # =&gt; true<\/pre>\n<p>\u3068\u3053\u308d\u3067\u3001\u5909\u66f4\u3055\u308c\u308b\u524d\u306e\u5024\u306f\u4f55\u3060\u3063\u305f\u306e\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p>\u305d\u3093\u306a\u6642\u306f\u3001\u300c\u5c5e\u6027\u540d_was\u300d\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">user.name_was # =&gt; \"\u305f\u308d\u3046\"<\/pre>\n<p>name \u306f\u4f55\u304b\u3089\u4f55\u306b\u5909\u66f4\u3055\u308c\u305f\u306e\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p>\u3068\u3044\u3046\u3068\u304d\u306b\u306f\u3001\u300c\u5c5e\u6027\u540d_change\u300d\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">user.name_change # =&gt; [\"\u305f\u308d\u3046\", \"\u306f\u306a\u3053\"]<\/pre>\n<p>changes \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u300c\u3069\u306e\u5c5e\u6027\u306e\u5024\u304c\u3001\u4f55\u304b\u3089\u4f55\u306b\u5909\u66f4\u3055\u308c\u305f\u306e\u304b\u300d\u3092\u307e\u3068\u3081\u3066\u77e5\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">user.changes # =&gt; {\"name\"=&gt;[\"\u305f\u308d\u3046\", \"\u306f\u306a\u3053\"]}<\/pre>\n<p>\u3072\u3068\u3064\u3060\u3051\u6ce8\u610f\u306a\u306e\u3067\u3059\u304c\u3001String#&lt;&lt; \u30e1\u30bd\u30c3\u30c9\u306a\u3069\u3067\u7834\u58ca\u7684\u306b\u5024\u3092\u5909\u66f4\u3057\u305f\u5834\u5408\u306f\u3001changed? \u306f false \u3092\u8fd4\u3057\u307e\u3059\u306e\u3067\u3054\u6ce8\u610f\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">user = User.find(1)\r\nuser.name # =&gt; \"\u305f\u308d\u3046\"\r\nuser.changed? # =&gt; false\r\nuser.name &lt;&lt; \"\u4e8c\u4e16\"\r\nuser.changed? # =&gt; false<\/pre>\n<p>\u300c\u5c5e\u6027\u540d_will_change!\u300d\u3092\u547c\u3073\u51fa\u3059\u3068\u3001\u305d\u306e\u5c5e\u6027\u306f\u5909\u66f4\u3055\u308c\u305f\u3082\u306e\u3068\u3057\u3066\u6271\u308f\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">user.name_will_change!\r\nuser.changed? # =&gt; true<\/pre>\n<p>ActiveRecord \u306b\u306f\u3001\u7d30\u304b\u3044\u4fbf\u5229\u6a5f\u80fd\u304c\u8272\u3005\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u308c\u3089\u306f\u7c21\u6f54\u306a\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u305f\u3081\u306e\u52a9\u3051\u306b\u306a\u3063\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<p>Rails \u306f\u751f\u7523\u6027\u304c\u9ad8\u3044\u3001\u3068\u8a00\u308f\u308c\u308b\u3053\u3068\u306f\u591a\u3044\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u3054\u7d39\u4ecb\u3057\u305f\u3088\u3046\u306a\u5730\u5473\u306a\u304c\u3089\u3082\u4fbf\u5229\u306a\u6a5f\u80fd\u304c\u76db\u308a\u3060\u304f\u3055\u3093\u306a\u3068\u3053\u308d\u304c\u3001\u305d\u306e\u6839\u62e0\u306e\u4e00\u3064\u3067\u306f\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>ActiveRecord \u306f\u4f7f\u3044\u52dd\u624b\u306e\u826f\u3044\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/p>\n<p>ActiveRecord::Base \u30af\u30e9\u30b9\u3092\u7d99\u627f\u3059\u308b\u3060\u3051\u3067\u3001\u307b\u3068\u3093\u3069\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u306a\u304f\u3001\u305d\u306e\u6069\u6075\u306b\u9810\u304b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2012\/12\/13\/rails%e3%81%a7%e5%a4%89%e6%9b%b4%e5%89%8d%e3%81%ae%e5%80%a4%e3%80%81%e5%9e%8b%e5%a4%89%e6%8f%9b%e5%89%8d%e3%81%ae%e5%80%a4%e3%80%81%e3%81%aa%e3%81%a9%e3%81%aa%e3%81%a9\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[22],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1068"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=1068"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1068\/revisions"}],"predecessor-version":[{"id":18004,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1068\/revisions\/18004"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=1068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=1068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=1068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}