{"id":1284,"date":"2012-12-25T22:58:10","date_gmt":"2012-12-25T13:58:10","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=1284"},"modified":"2018-11-14T16:33:58","modified_gmt":"2018-11-14T07:33:58","slug":"rails%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%ab%e3%83%90%e3%83%83%e3%82%af%e3%81%be%e3%81%a8%e3%82%81","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2012\/12\/25\/rails%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%ab%e3%83%90%e3%83%83%e3%82%af%e3%81%be%e3%81%a8%e3%82%81\/","title":{"rendered":"Rails\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u307e\u3068\u3081"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>Techscore \u672c\u4f53\u306e\u8a18\u4e8b\u306e\u4e0b\u66f8\u304d\u3092\u66f8\u304d\u9032\u3081\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306e\u8a18\u4e8b\u306e\u4e0b\u66f8\u304d\u3092\u516c\u958b\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306e\u7a2e\u985e<\/h2>\n<p>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3068\u306f\u3001\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306e\u5b9f\u884c\u3084\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3078\u306e\u4fdd\u5b58\u306a\u3069\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u51e6\u7406\u3092\u884c\u3046\u305f\u3081\u306e\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n<p>\u3042\u308b\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u5fc5\u305a\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u51e6\u7406\u3092\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306b\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001\u30e2\u30c7\u30eb\u306e\u4e00\u8cab\u6027\u3092\u4fdd\u3064\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>Rails \u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306e\u7a2e\u985e\u3092\u307e\u3068\u3081\u3066\u307f\u308b\u3068\u3001\u975e\u5e38\u306b\u591a\u3044\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>after_find<\/h3>\n<p>\u691c\u7d22\u30e1\u30bd\u30c3\u30c9\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u898b\u3064\u304b\u3063\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u691c\u7d22\u6761\u4ef6\u3092\u6307\u5b9a\u305b\u305a\u306b all \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u306a\u3069\u3001\u691c\u7d22\u7d50\u679c\u304c\u5927\u91cf\u306b\u306a\u308b\u5834\u5408\u306f\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<p>\u691c\u7d22\u7d50\u679c\u306e\u6570\u3060\u3051\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u306e\u3067\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u91cd\u5927\u306a\u5f71\u97ff\u3092\u53ca\u307c\u3059\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>after_initialize<\/h3>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3055\u308c\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>User.new \u306e\u3088\u3046\u306b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3057\u305f\u5834\u5408\u306f\u3001after_find \u306f\u5b9f\u884c\u3055\u308c\u305a\u306b after_initialize \u306e\u307f\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>after_find \u3068\u540c\u69d8\u3001\u691c\u7d22\u6761\u4ef6\u3092\u6307\u5b9a\u305b\u305a\u306b all \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u306a\u3069\u3001\u691c\u7d22\u7d50\u679c\u304c\u5927\u91cf\u306b\u306a\u308b\u5834\u5408\u306f\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<p>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3055\u308c\u305f\u5206\u3060\u3051\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u306e\u3067\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u91cd\u5927\u306a\u5f71\u97ff\u3092\u53ca\u307c\u3059\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>before_validation<\/h3>\n<p>\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u304c\u884c\u308f\u308c\u308b\u76f4\u524d\u3067\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u4e3b\u306b\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u524d\u306b\u5c5e\u6027\u5024\u3092\u5fae\u8abf\u6574\u3059\u308b\u5834\u5408\u306b\u7528\u3044\u3089\u308c\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u306f\u5c5e\u6027\u5024\u306b\u3069\u306e\u3088\u3046\u306a\u578b\u306e\u3069\u306e\u3088\u3046\u306a\u5024\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u304b\u4fdd\u8a3c\u306f\u7121\u3044\u3053\u3068\u306b\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u4e0a\u3067\u306f\u6587\u5b57\u5217\u578b\u3067\u3042\u3063\u3066\u3082\u3001\u6570\u5024\u304c\u4ee3\u5165\u3055\u308c\u3066\u3044\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3057\u3001nil \u3067\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u306e\u3067\u3001\u60f3\u5b9a\u5916\u306e\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u306a\u3044\u3088\u3046\u306b\u6c17\u3092\u4ed8\u3051\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>after_validation<\/h3>\n<p>\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u304c\u884c\u308f\u308c\u305f\u76f4\u5f8c\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306b\u6210\u529f\u3057\u305f\u5f8c\u3067\u306f\u306a\u304f\u3001\u5358\u7d14\u306b\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u304c\u884c\u306a\u308f\u308c\u305f\u5f8c\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001before_validation \u3068\u540c\u69d8\u306b\u3001\u5c5e\u6027\u5024\u306b\u3069\u306e\u3088\u3046\u306a\u578b\u306e\u5024\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u304b\u5206\u304b\u308a\u307e\u305b\u3093\u3057\u3001nil \u3067\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>before_save<\/h3>\n<p>\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306b\u6210\u529f\u3057\u3001\u5b9f\u969b\u306b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u4fdd\u5b58\u3055\u308c\u308b\u76f4\u524d\u3067\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>INSERT \u3055\u308c\u308b\u5834\u5408\u3082\u3001UPDATE \u3055\u308c\u308b\u5834\u5408\u3082\u547c\u3073\u51fa\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>INSERT \u3082\u3057\u304f\u306f UPDATE \u306e\u5834\u5408\u3060\u3051\u5b9f\u884c\u3057\u305f\u3044\u51e6\u7406\u304c\u3042\u308b\u3068\u304d\u306f\u3001\u5f8c\u8ff0\u3059\u308b before_create \/ before_update \u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>before_create \/ before_update<\/h3>\n<p>before_save \u306e\u5f8c\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u767b\u9332\u3055\u308c\u308b\u3068\u304d (new_record? \u304c true \u306e\u3068\u304d) \u306f before_create \u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u66f4\u65b0\u3055\u308c\u308b\u3068\u304d (new_record? \u304c false \u306e\u3068\u304d) \u306f before_update \u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u767b\u9332\u3068\u66f4\u65b0\u306e\u3069\u3061\u3089\u306e\u5834\u5408\u306b\u3082\u540c\u3058\u51e6\u7406\u3092\u884c\u3046\u306e\u3067\u3042\u308c\u3070\u3001before_save \u3092\u4f7f\u7528\u3059\u308b\u3068\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>after_create \/ after_update<\/h3>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u4fdd\u5b58\u3055\u308c\u305f\u76f4\u5f8c (after_save \u306e\u76f4\u524d) \u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u767b\u9332\u3055\u308c\u305f\u3068\u304d\u306f after_create\u3001\u66f4\u65b0\u3055\u308c\u305f\u3068\u304d\u306f after_update \u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u767b\u9332\u3068\u66f4\u65b0\u306e\u3069\u3061\u3089\u306e\u5834\u5408\u306b\u3082\u540c\u3058\u51e6\u7406\u3092\u884c\u3046\u306e\u3067\u3042\u308c\u3070\u3001after_save \u3092\u4f7f\u7528\u3059\u308b\u3068\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>after_save<\/h3>\n<p>after_create \/ after_update \u306e\u76f4\u5f8c\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3078\u306e COMMIT \u306e\u76f4\u524d\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u4fdd\u5b58\u3055\u308c\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u95a2\u9023\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u64cd\u4f5c\u3059\u308b\u306a\u3069\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3067\u8a00\u3046\u30c8\u30ea\u30ac\u30fc\u306e\u3088\u3046\u306a\u51e6\u7406\u3092\u884c\u306a\u3046\u5834\u5408\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>after_commit<\/h3>\n<p>after_save \u306e\u5f8c\uff08\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b COMMIT \u3055\u308c\u305f\u5f8c\uff09\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>after_rollback<\/h3>\n<p>\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30a8\u30e9\u30fc\u3084 SQL \u5b9f\u884c\u6642\u306b\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>after_touch<\/h3>\n<p>touch \u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3073\u51fa\u3055\u308c\u305f\u76f4\u5f8c\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u5834\u5408\u306f before_save \u3084 after_save \u306a\u3069\u4ed6\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306f\u5b9f\u884c\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>before_destroy<\/h3>\n<p>destroy \u30e1\u30bd\u30c3\u30c9\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u524a\u9664\u3055\u308c\u308b\u76f4\u524d\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u524a\u9664\u306e\u5834\u5408\u306f before_destroy \u2192 after_destroy \u2192 after_commit \u306e\u9806\u756a\u3067\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u6ce8\u610f\u3068\u3057\u3066 delete\/delete_all \u30e1\u30bd\u30c3\u30c9\u3067\u524a\u9664\u3057\u305f\u5834\u5408\u306f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u547c\u3073\u51fa\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>after_destroy<\/h3>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u524a\u9664\u3055\u308c\u305f\u76f4\u5f8c\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5916\u3067\u7ba1\u7406\u3057\u3066\u3044\u308b\u30ea\u30bd\u30fc\u30b9\u3092\u524a\u9664\u3059\u308b\u5834\u5408\u306a\u3069\u306b\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u5b9f\u884c\u3055\u308c\u308b\u30bf\u30a4\u30df\u30f3\u30b0<\/h2>\n<p>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306f\u5e38\u306b\u5b9f\u884c\u3055\u308c\u308b\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u66f4\u65b0\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3042\u3063\u3066\u3082\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u5e38\u306b\u5b9f\u884c\u3055\u308c\u306a\u3044\u3082\u306e\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>create<\/li>\n<li>create!<\/li>\n<li>decrement!<\/li>\n<li>destroy<\/li>\n<li>destroy_all<\/li>\n<li>increment!<\/li>\n<li>save<\/li>\n<li>save!<\/li>\n<li>save(:validate =&gt; false)<\/li>\n<li>toggle!<\/li>\n<li>update<\/li>\n<li>update_attribute<\/li>\n<li>update_attributes<\/li>\n<li>update_attributes!<\/li>\n<li>valid?<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u306e\u30e1\u30bd\u30c3\u30c9\u306f after_find \u53ca\u3073 after_initialize \u3092\u5b9f\u884c\u3057\u307e\u3059\uff08after_initialize \u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092 new \u3057\u305f\u3068\u304d\u306b\u3082\u5b9f\u884c\u3055\u308c\u307e\u3059\uff09\u3002<\/p>\n<ul>\n<li>all<\/li>\n<li>first<\/li>\n<li>find<\/li>\n<li>find_all_by_attribute<\/li>\n<li>find_by_attribute<\/li>\n<li>find_by_attribute!<\/li>\n<li>last<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u5b9f\u884c\u3057\u307e\u305b\u3093\u3002<\/p>\n<ul>\n<li>decrement<\/li>\n<li>decrement_counter<\/li>\n<li>delete<\/li>\n<li>delete_all<\/li>\n<li>find_by_sql<\/li>\n<li>increment<\/li>\n<li>increment_counter<\/li>\n<li>toggle<\/li>\n<li>touch<\/li>\n<li>update_column<\/li>\n<li>update_all<\/li>\n<li>update_counters<\/li>\n<\/ul>\n<p>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3067\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u306b\u95a2\u308f\u308b\u51e6\u7406\u3092\u884c\u3063\u3066\u3044\u308b\u5834\u5408\u306a\u3069\u306b\u306f\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u30b9\u30ad\u30c3\u30d7\u3055\u308c\u308b\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306e\u8a2d\u5b9a<\/h2>\n<p>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u8a2d\u5b9a\u3059\u308b\u65b9\u6cd5\u306f\u4f55\u901a\u308a\u304b\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u4e00\u3064\u76ee\u306f\u5b9f\u884c\u3057\u305f\u3044\u51e6\u7406\u3092\u30d6\u30ed\u30c3\u30af\u3067\u6307\u5b9a\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u4fdd\u5b58\u3055\u308c\u308b\u524d\u306b\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u30cf\u30c3\u30b7\u30e5\u5316\u3057\u305f\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \">class User &lt; ActiveRecord::Base\r\n\r\n  before_save do\r\n    # \u5165\u529b\u3055\u308c\u305f\u5e73\u6587\u306e\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u30cf\u30c3\u30b7\u30e5\u5316\u3059\u308b.\r\n    if self.password.present?\r\n      self.hashed_password = ... # \u30cf\u30c3\u30b7\u30e5\u5024\u3092\u6c42\u3081\u308b.\r\n    end\r\n  end\r\n\r\nend<\/pre>\n<p>\u4e8c\u3064\u76ee\u306f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3057\u305f\u3044\u30e1\u30bd\u30c3\u30c9\u540d\u3092\u30b7\u30f3\u30dc\u30eb\u3067\u6307\u5b9a\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<p>\u51e6\u7406\u5185\u5bb9\u304c\u591a\u3044\u5834\u5408\u3084\u3001\u4ed6\u306e\u7b87\u6240\u304b\u3089\u3082\u547c\u3073\u51fa\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u5834\u5408\u306f\u3053\u306e\u65b9\u6cd5\u304c\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class User &lt; ActiveRecord::Base\r\n\r\n  before_save :hash_password\r\n\r\n  # \u5165\u529b\u3055\u308c\u305f\u5e73\u6587\u306e\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u30cf\u30c3\u30b7\u30e5\u5316\u3059\u308b.\r\n  def hash_password\r\n    if self.password.present?\r\n      self.hashed_password = ... # \u30cf\u30c3\u30b7\u30e5\u5024\u3092\u6c42\u3081\u308b.\r\n    end\r\n  end\r\nend<\/pre>\n<p>\u4e09\u3064\u76ee\u306f\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u51e6\u7406\u3092\u3082\u3064\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6307\u5b9a\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u65b9\u6cd5\u306f\u3001\u540c\u3058\u51e6\u7406\u3092\u8907\u6570\u306e\u30e2\u30c7\u30eb\u3067\u5171\u6709\u3057\u305f\u3044\u5834\u5408\u306b\u4f7f\u7528\u3059\u308b\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class User &lt; ActiveRecord::Base\r\n\r\n  before_save HashPassword.new\r\n\r\nend\r\n\r\nclass HashPassword\r\n\r\n  def before_save(object)\r\n    if object.password.present?\r\n      object.hashed_password = ... # \u30cf\u30c3\u30b7\u30e5\u5024\u3092\u6c42\u3081\u308b.\r\n    end \r\n  end \r\n\r\nend<\/pre>\n<p>\u3061\u306a\u307f\u306b\u3067\u3059\u304c\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8907\u6570\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class User &lt; ActiveRecord::Base\r\n\r\n  before_save do\r\n    puts 'before_save 1'\r\n  end \r\n\r\n  before_save do\r\n    puts 'before_save 2'\r\n  end \r\n\r\nend<\/pre>\n<p>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u8907\u6570\u6307\u5b9a\u3057\u305f\u5834\u5408\u306f\u3001\u6307\u5b9a\u3057\u305f\u9806\u756a\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u4e0a\u8a18\u306e\u901a\u308a\u306b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u8a2d\u5b9a\u3057\u305f\u72b6\u614b\u3067 User \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092 save \u3059\u308b\u3068\u3001\u300cbefore_save 1\u300d\u300cbefore_save 2\u300d\u306e\u9806\u306b\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">&gt; user = User.new\r\n&gt; user.save\r\nbefore_save 1\r\nbefore_save 2<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u6761\u4ef6\u4ed8\u304d\u30b3\u30fc\u30eb\u30d0\u30c3\u30af<\/h2>\n<p>\u3042\u308b\u6761\u4ef6\u3092\u6e80\u305f\u3057\u305f\u5834\u5408\u3060\u3051\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u5b9f\u884c\u3057\u305f\u3044\u5834\u5408\u306f :if \u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3057\u307e\u3059\uff08:unless \u30aa\u30d7\u30b7\u30e7\u30f3\u3082\u3042\u308a\u307e\u3059\uff09\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class User &lt; ActiveRecord::Base\r\n\r\n  before_save :hash_password, :if =&gt; :password_required?\r\n\r\n  # \u5165\u529b\u3055\u308c\u305f\u5e73\u6587\u306e\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u30cf\u30c3\u30b7\u30e5\u5316\u3059\u308b. \r\n  def hash_password\r\n    self.hashed_password = ... # \u30cf\u30c3\u30b7\u30e5\u5024\u3092\u6c42\u3081\u308b.\r\n  end\r\n\r\n  # \u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u5165\u529b\u304c\u5fc5\u8981\u3067\u3042\u308b\u304b\u5224\u5b9a\u3059\u308b.\r\n  def password_required?\r\n    ...\r\n  end\r\n\r\nend<\/pre>\n<p>\u5148\u307b\u3069\u306e\u4f8b\u3067\u306f hash_password \u306e\u4e2d\u3067\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u304b\u5224\u5b9a\u3057\u3066\u3044\u307e\u3057\u305f\u304c\u3001\u3053\u3053\u3067\u306f\u5224\u5b9a\u51e6\u7406\u3092 password_required? \u30e1\u30bd\u30c3\u30c9\u3067\u884c\u306a\u308f\u308c\u307e\u3059\u3002<\/p>\n<p>:if \u30aa\u30d7\u30b7\u30e7\u30f3\u306b\u306f\u30e1\u30bd\u30c3\u30c9\u540d\u3067\u306f\u306a\u304f Proc \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">before_save :hash_password, :if =&gt; lambda { self.password.present? }<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u4fdd\u5b58\u51e6\u7406\u306e\u4e2d\u65ad<\/h2>\n<p>before_validation \u3067\u6307\u5b9a\u3057\u305f\u51e6\u7406\u304c false \u3092\u8fd4\u3057\u305f\u5834\u5408\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u4fdd\u5b58\u51e6\u7406\u306f\u4e2d\u65ad\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u6700\u5f8c\u306b\u5b9f\u884c\u3057\u305f\u51e6\u7406\u304c\u610f\u56f3\u305b\u305a false \u3068\u306a\u3089\u306a\u3044\u3088\u3046\u306b\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u4fdd\u5b58\u51e6\u7406\u304c\u4e2d\u65ad\u3055\u308c\u308b\u3068\u3001\u305d\u308c\u4ee5\u964d\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306f\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001save \u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3073\u51fa\u3055\u308c\u3066\u3044\u305f\u5834\u5408\u306f\u3001\u547c\u3073\u51fa\u3057\u5143\u306b false \u304c\u8fd4\u3055\u308c\u307e\u3059\uff08save \u304c false \u3092\u8fd4\u3057\u307e\u3059\uff09\u3002<\/p>\n<p>save! \u304c\u547c\u3073\u51fa\u3055\u308c\u3066\u3044\u305f\u5834\u5408\u306f\u4f8b\u5916 ActiveRecord::RecordInvalid \u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u51e6\u7406\u3092\u6d3b\u7528\u3059\u308b\u3068\u3001\u30e2\u30c7\u30eb\u306e\u4e00\u8cab\u6027\u3092\u7c21\u5358\u306b\u4fdd\u3064\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4e00\u65b9\u3067\u3001destroy \u30e1\u30bd\u30c3\u30c9\u306f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u5b9f\u884c\u3059\u308b\u3001delete \u30e1\u30bd\u30c3\u30c9\u306f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u5b9f\u884c\u3057\u306a\u3044\u3001update_attribute \u3082\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u5b9f\u884c\u3057\u306a\u3044\u3001\u30fb\u30fb\u30fb\u3068\u3044\u3046\u6c7a\u307e\u308a\u3082\u7406\u89e3\u3057\u3066\u304a\u304f\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\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>Techscore \u672c\u4f53\u306e\u8a18\u4e8b\u306e\u4e0b\u66f8\u304d\u3092\u66f8\u304d\u9032\u3081\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306e\u8a18\u4e8b\u306e\u4e0b\u66f8\u304d\u3092\u516c\u958b\u3057\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2012\/12\/25\/rails%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%ab%e3%83%90%e3%83%83%e3%82%af%e3%81%be%e3%81%a8%e3%82%81\/\">\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\/1284"}],"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=1284"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1284\/revisions"}],"predecessor-version":[{"id":17995,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1284\/revisions\/17995"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=1284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=1284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=1284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}