{"id":1296,"date":"2012-12-25T22:58:35","date_gmt":"2012-12-25T13:58:35","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=1296"},"modified":"2018-11-14T16:33:58","modified_gmt":"2018-11-14T07:33:58","slug":"rails%e3%81%ae%e3%82%aa%e3%83%96%e3%82%b6%e3%83%bc%e3%83%90%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%aa%e3%83%96%e3%82%b6%e3%83%bc%e3%83%90%e3%81%be%e3%81%a8%e3%82%81\/","title":{"rendered":"Rails\u306e\u30aa\u30d6\u30b6\u30fc\u30d0\u307e\u3068\u3081"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f Rails \u306e\u30aa\u30d6\u30b6\u30fc\u30d0\u306e\u8a71\u3092\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u30aa\u30d6\u30b6\u30fc\u30d0\u3068\u306f<\/h2>\n<p>Rails \u306e\u30aa\u30d6\u30b6\u30fc\u30d0\u3068\u306f\u3001\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u306e Observer \u30d1\u30bf\u30fc\u30f3\u3092\u5b9f\u73fe\u3059\u308b\u4ed5\u7d44\u307f\u3067\u3059\u3002<\/p>\n<p>\u53ef\u80fd\u306a\u3053\u3068\u306f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3068\u540c\u3058\u3067\u3001\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3084\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3078\u306e\u4fdd\u5b58\u304c\u884c\u308f\u308c\u308b\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u884c\u3046\u51e6\u7406\u3092\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3068\u30aa\u30d6\u30b6\u30fc\u30d0\u306e\u3069\u3061\u3089\u3092\u5229\u7528\u3059\u308b\u304b\u306f\u3001\u305d\u306e\u51e6\u7406\u304c\u30e2\u30c7\u30eb\u306e\u4e00\u8cab\u6027\u3092\u4fdd\u3064\u305f\u3081\u306b\u5fc5\u8981\u3067\u3042\u308b\u304b\u3068\u3044\u3046\u57fa\u6e96\u3067\u8003\u3048\u308b\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u4fdd\u5b58\u524d\u306b\u5165\u529b\u3055\u308c\u305f\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u30cf\u30c3\u30b7\u30e5\u5316\u3059\u308b\u306e\u3067\u3042\u308c\u3070\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3001\u4f1a\u54e1\u767b\u9332\u5f8c\u306b\u767b\u9332\u5b8c\u4e86\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u3059\u308b\u3088\u3046\u306a\u5834\u5408\u306f\u30aa\u30d6\u30b6\u30fc\u30d0\u304c\u9069\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u30aa\u30d6\u30b6\u30fc\u30d0\u306e\u4f5c\u6210<\/h2>\n<p>\u30aa\u30d6\u30b6\u30fc\u30d0\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a ActiveRecord::Observer \u3092\u7d99\u627f\u3057\u305f\u30af\u30e9\u30b9\u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class UserObserver &lt; ActiveRecord::Observer\r\n\r\nend<\/pre>\n<p>\u30bd\u30fc\u30b9\u30d5\u30a1\u30a4\u30eb\u306f app\/models \u4ee5\u4e0b\u306b\u4fdd\u5b58\u3057\u307e\u3059\u3002<\/p>\n<p>scaffold \u3092\u5229\u7528\u3059\u308b\u306e\u3067\u3042\u308c\u3070\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u30aa\u30d6\u30b6\u30fc\u30d0\u3092\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">rails generate observer \uff1c\u30e2\u30c7\u30eb\u540d\uff1e<\/pre>\n<p>\u4f8b\u3048\u3070\u3001User \u30e2\u30c7\u30eb\u306e\u30aa\u30d6\u30b6\u30fc\u30d0\u3092\u4f5c\u6210\u3059\u308b\u306e\u3067\u3042\u308c\u3070\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">rails generate observer User<\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3001app\/models\/user_observer.rb \u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u30aa\u30d6\u30b6\u30fc\u30d0\u306e\u5b9f\u88c5\u306f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3068\u540c\u3058\u3067\u3001before_save \u3084 after_create \u306a\u3069\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class UserObserver &lt; ActiveRecord::Observer\r\n\r\n  def after_create(user)\r\n    # ... \u30e6\u30fc\u30b6\u304c\u4f5c\u6210 (= \u4f1a\u54e1\u767b\u9332) \u3055\u308c\u305f\u306e\u3067, \u767b\u9332\u5b8c\u4e86\u30e1\u30fc\u30eb\u3092\u9001\u308b.\r\n  end\r\n\r\n  def after_destroy(user)\r\n    # ... \u30e6\u30fc\u30b6\u304c\u524a\u9664 (= \u9000\u4f1a) \u3055\u308c\u305f\u306e\u3067, \u500b\u4eba\u60c5\u5831\u306b\u95a2\u3059\u308b\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b.\r\n  end\r\n\r\nend<\/pre>\n<p>\u4f55\u3089\u304b\u306e\u4e8b\u60c5\u3067\u30e2\u30c7\u30eb\u3068\u30aa\u30d6\u30b6\u30fc\u30d0\u306e\u540d\u524d\u3092\u5408\u308f\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b observe \u30e1\u30bd\u30c3\u30c9\u3067\u5bfe\u8c61\u3068\u3059\u308b\u30e2\u30c7\u30eb\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class MemberObserver &lt; ActiveRecord::Observer\r\n\r\n  observe :user\r\n\r\n  ...\r\n\r\nend<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u30aa\u30d6\u30b6\u30fc\u30d0\u306e\u8a2d\u5b9a<\/h2>\n<p>\u4f5c\u6210\u3057\u305f\u30aa\u30d6\u30b6\u30fc\u30d0\u3092\u6709\u52b9\u306b\u3059\u308b\u306b\u306f\u3001config\/application.rb \u3067\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">config.active_record.observers = [:user_observer]<\/pre>\n<p>\u5b9f\u884c\u74b0\u5883\u306b\u3088\u3063\u3066\u30aa\u30d6\u30b6\u30fc\u30d0\u306e\u6709\u52b9\/\u7121\u52b9\u3092\u5207\u308a\u66ff\u3048\u305f\u3044\u5834\u5408\u306f\u3001config\/environments \u4ee5\u4e0b\u306e\u74b0\u5883\u3054\u3068\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u8907\u6570\u306e\u30e2\u30c7\u30eb\u3092\u5bfe\u8c61\u3068\u3059\u308b\u30aa\u30d6\u30b6\u30fc\u30d0<\/h2>\n<p>\u8907\u6570\u306e\u30e2\u30c7\u30eb\u306b\u5bfe\u3057\u3066\u3001\u540c\u3058\u30aa\u30d6\u30b6\u30fc\u30d0\u3092\u8a2d\u5b9a\u3057\u305f\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u30d6\u30ed\u30b0\u8a18\u4e8b\u3084\u5546\u54c1\u8cfc\u5165\u5c65\u6b74\u306a\u3069\u306e\u30e2\u30c7\u30eb\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># \u30d6\u30ed\u30b0\u8a18\u4e8b.\r\nclass Article &lt; ActiveRecord::Base\r\n  ...\r\nend\r\n\r\n# \u5546\u54c1\u8cfc\u5165\u5c65\u6b74.\r\nclass Purchase &lt; ActiveRecord::Base\r\n  ...\r\nend<\/pre>\n<p>\u305d\u3057\u3066\u3001\u3053\u308c\u3089\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u767b\u9332\u3055\u308c\u305f\u3068\u304d (= \u30d6\u30ed\u30b0\u8a18\u4e8b\u306e\u6295\u7a3f\u3084\u5546\u54c1\u3092\u8cfc\u5165\u3057\u305f\u3068\u304d) \u306b\u30dd\u30a4\u30f3\u30c8\u3092\u4ed8\u4e0e\u3059\u308b\u30aa\u30d6\u30b6\u30fc\u30d0\u3092\u4f5c\u6210\u3059\u308b\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b observe \u30e1\u30bd\u30c3\u30c9\u3067\u5bfe\u8c61\u3068\u3059\u308b\u30e2\u30c7\u30eb\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># \u30e6\u30fc\u30b6\u306e\u7279\u5b9a\u306e\u884c\u52d5\u306b\u5bfe\u3057\u3066\u30dd\u30a4\u30f3\u30c8\u3092\u4ed8\u4e0e\u3059\u308b\u30aa\u30d6\u30b6\u30fc\u30d0.\r\nclass PointingObserver &lt; ActiveRecord::Observer\r\n\r\n  observe :article, :news\r\n\r\n  def after_create(object)\r\n    # ... \u30e6\u30fc\u30b6\u306b\u30dd\u30a4\u30f3\u30c8\u3092\u4ed8\u4e0e\u3059\u308b.\r\n  end\r\n\r\nend<\/pre>\n<p>\u3053\u3046\u3059\u308b\u3053\u3068\u3067\u3001\u4e00\u3064\u306e\u30aa\u30d6\u30b6\u30fc\u30d0\u3092\u8907\u6570\u306e\u30e2\u30c7\u30eb\u306b\u7d50\u3073\u4ed8\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u4eca\u56de\u306f\u30aa\u30d6\u30b6\u30fc\u30d0\u306e\u8a71\u3092\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3068\u30aa\u30d6\u30b6\u30fc\u30d0\u306f\u3069\u3061\u3089\u3082\u540c\u3058\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u306e\u3067\u3001\u3069\u3061\u3089\u3092\u4f7f\u3046\u3079\u304d\u304b\u898b\u6975\u3081\u305f\u4e0a\u3067\u6d3b\u7528\u3057\u307e\u3057\u3087\u3046\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>\u4eca\u56de\u306f Rails \u306e\u30aa\u30d6\u30b6\u30fc\u30d0\u306e\u8a71\u3092\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<br \/>\n\u30aa\u30d6\u30b6\u30fc\u30d0\u3068\u306f<br \/>\nRails \u306e\u30aa\u30d6\u30b6\u30fc\u30d0\u3068\u306f\u3001\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u306e Observer \u30d1\u30bf\u30fc\u30f3\u3092\u5b9f\u73fe\u3059\u308b\u4ed5\u7d44\u307f\u3067\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2012\/12\/25\/rails%e3%81%ae%e3%82%aa%e3%83%96%e3%82%b6%e3%83%bc%e3%83%90%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\/1296"}],"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=1296"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1296\/revisions"}],"predecessor-version":[{"id":17994,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1296\/revisions\/17994"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=1296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=1296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=1296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}