{"id":7687,"date":"2019-11-30T23:58:17","date_gmt":"2019-11-30T14:58:17","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=7687"},"modified":"2023-05-27T22:57:44","modified_gmt":"2023-05-27T13:57:44","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%83%a2%e3%83%87%e3%83%ab%e5%90%8c%e5%a3%ab%e3%82%92%e9%96%a2%e9%80%a3%e3%81%a5%e3%81%91%e3%82%8b%e3%81%ab%e3%81%af%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=7687","title":{"rendered":"\u3010Ruby on Rails\u3011\u30e2\u30c7\u30eb\u540c\u58eb\u3092\u95a2\u9023\u3065\u3051\u3092\u4fbf\u5229\u306b\u6271\u3046\u6a5f\u80fd"},"content":{"rendered":"<p>\u3053\u3053\u3067\u3054\u7d39\u4ecb\u3059\u308b\u306e\u306f\u3042\u304f\u307e\u3067Rails\u3067\u30e2\u30c7\u30eb\u540c\u58eb\u306e\u95a2\u9023\u3065\u3051\u304c\u3042\u3063\u305f\u5834\u5408\u306b\u4fbf\u5229\u306b\u6271\u3046\u6a5f\u80fd\u306e\u3054\u7d39\u4ecb\u3067\u3042\u308a<span style=\"color: #ff0000;\"><strong>\u7d76\u5bfe\u306b\u95a2\u9023\u3065\u3051\u304c\u3042\u308b\u969b\u306b\u5b9a\u7fa9\u3057\u306a\u3051\u308c\u3070\u52d5\u304b\u306a\u3044\u3068\u3044\u3046\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u77e5\u3063\u3066\u304a\u304f\u3068\u975e\u5e38\u306b\u958b\u767a\u52b9\u7387\u304c\u4e0a\u304c\u308b\u6a5f\u80fd<\/strong><\/span>\u306a\u306e\u3067\u77e5\u8b58\u3068\u3057\u3066\u77e5\u3063\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>1:1\u306e\u5834\u5408<\/h2>\n<pre class=\"lang:default decode:true \">has_one:<\/pre>\n<h2>\u591a:1\u306b\u306a\u308b\u5834\u5408<\/h2>\n<p>\u305d\u308c\u305e\u308c\u306e\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\uff08app\/models\/\u30e2\u30c7\u30eb\u540d.rb)\u306b\u5bfe\u3057\u3066\u4e0b\u8a18\u306e\u8a18\u8ff0\u3092\u3057\u307e\u3059\u3002<\/p>\n<h3>\u591a\u306e\u65b9\u306e\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb<\/h3>\n<pre class=\"lang:default decode:true\">belongs_to:1\u306e\u65b9\u306e\u30e2\u30c7\u30eb\u540d<\/pre>\n<p>belongs_to\u306f\u5916\u90e8\u30ad\u30fc\u3068\u3057\u3066\u62b1\u3048\u3066\u3044\u308b\u305f\u3060\u4e00\u4eba\u3060\u3051\u5b58\u5728\u3059\u308b\u89aa\u30e2\u30c7\u30eb\u3092\u6307\u5b9a\u3059\u308b\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002<\/p>\n<h3>1\u306e\u65b9\u306e\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb<\/h3>\n<pre class=\"lang:default decode:true\">has_many :\u591a\u306e\u65b9\u306e\u30e2\u30c7\u30eb\u540d<\/pre>\n<p>has_many\u306f\u5916\u90e8\u30ad\u30fc\u3068\u3057\u3066\u30ea\u30f3\u30af\u3057\u3066\u3044\u308b\u4ed6\u306e\u30e2\u30c7\u30eb\u304c\u8907\u6570\u767b\u9332\u304c\u53ef\u80fd\u3067\u3042\u308b\u3053\u3068\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u305f\u304f\u3055\u3093\u62b1\u3048\u308b\u5b50\u4f9b\u3092\u6307\u5b9a\u3059\u308b\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002\u306a\u304a\u3053\u3053\u306b\u8a18\u8ff0\u3059\u308b\u30e2\u30c7\u30eb\u540d\u306f<span style=\"color: #ff0000;\"><strong>\u30e2\u30c7\u30eb\u540d\u3092\u8907\u6570\u5f62\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/strong><\/span><\/p>\n<h4>\u89aa\uff081\u306e\u65b9\u306e\u30e2\u30c7\u30eb\uff09\u304c\u524a\u9664\u3055\u308c\u305f\u3089\u7d10\u3065\u304f\u5b50\uff08\u591a\u306e\u65b9\u306e\u30e2\u30c7\u30eb\uff09\u3092\u524a\u9664\u3059\u308b\u3002<\/h4>\n<p>\u89aa\u304c\u524a\u9664\u3055\u308c\u305f\u3089\u5b50\u3082\u524a\u9664\u3059\u308b\u8a18\u8ff0\u65b9\u6cd5\u306f\u3053\u3061\u3089\u306b\u306a\u308a\u307e\u3059\u3002delete_all\u306b\u6bd4\u3079\u305f\u30891\u4ef61\u4ef6\u524a\u9664\u3059\u308b\u306e\u3067\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306f\u60aa\u3044\u3067\u3059\u304c\u3001\u95a2\u9023\u3065\u3051\u306e\u3055\u3089\u306b\u95a2\u9023\u3065\u3051\u307e\u3067\u524a\u9664\u3067\u304d\u305f\u308a\u3059\u308b\u306e\u3067\u4f7f\u3044\u5206\u3051\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">dependent: :destroy<\/pre>\n<p>\u95a2\u9023\u3065\u3051\u3092\u307e\u3068\u3081\u3066\u524a\u9664\u304c\u3067\u304d\u308b\u306e\u3067destroy\u306b\u6bd4\u3079\u308b\u3068\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u826f\u3044\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">dependent: :delete_all<\/pre>\n<p>\u307e\u305f\u3001\u3053\u308c\u3089\u306e<span style=\"color: #ff0000;\"><strong>dependent\u30e1\u30bd\u30c3\u30c9\u304c\u6a5f\u80fd\u3059\u308b\u306e\u306fdestroy\u30e1\u30bd\u30c3\u30c9\u3067\u306e\u524a\u9664\u306e\u5834\u5408\u3060\u3051<\/strong><\/span>\u306b\u306a\u308b\u306e\u3067\u305d\u306e\u70b9\u3082\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002(delete\u30e1\u30bd\u30c3\u30c9\u3067\u306f\u6a5f\u80fd\u3057\u307e\u305b\u3093\u3002\uff09<\/p>\n<h3>\u5b9f\u969b\u306b\u30d7\u30ed\u30b0\u30e9\u30e0\u5185\u3067\u4f7f\u3046\u306b\u306f\uff1f<\/h3>\n<p>\u4e0a\u8a18\u306e\u3088\u3046\u306a\u6307\u5b9a\u3092\u3059\u308b\u3053\u3068\u3067\u4e0b\u8a18\u306e\u6307\u5b9a\u3092\u3059\u308b\u3053\u3068\u3067<span style=\"color: #ff0000;\"><strong>\u89aa\u30e2\u30c7\u30eb\u306b\u7d10\u3065\u304f\u5b50\u30e2\u30c7\u30eb\u3092\u5168\u3066\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true \">\u89aa\u30e2\u30c7\u30eb(1).\u5b50\u30e2\u30c7\u30eb(\u591a)<\/pre>\n<p>\u4f8b\u3048\u3070\u3001\u4e00\u610f\u306b\u6c7a\u307e\u308bUser\u30e2\u30c7\u30eb\u306b\u7d10\u3065\u304f\u3001Work\u30e2\u30c7\u30eb\u304c\u3042\u3063\u305f\u3068\u3057\u305f\u3089user.works\u3068\u884c\u3063\u305f\u30e1\u30bd\u30c3\u30c9\u3067\u5168\u3066\u306eUser\u30e2\u30c7\u30eb\u306b\u7d10\u3065\u304fWork\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u591a\uff1a\u591a\u306e\u5834\u5408<\/h2>\n<h3>\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5<\/h3>\n<p>\u6700\u3082\u4e00\u822c\u7684\u306a\u65b9\u6cd5\u306b\u306a\u308a\u307e\u3059\u3002\u30c6\u30fc\u30d6\u30eb\u9593\u306e\u7d10\u4ed8\u3051\u3092\u7ba1\u7406\u3059\u308b<span style=\"color: #ff0000;\"><strong>\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb<\/strong><\/span>\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u3068\uff11\u306e\u65b9\u306e\u30c6\u30fc\u30d6\u30eb\u306b<span style=\"color: #ff0000;\"><strong>\u300chas_many\u300d<\/strong><\/span>\u3092\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u306ehas_many\u306b<span style=\"color: #ff0000;\"><strong>\u300c:through\u300d<\/strong><\/span>\u3092\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u67d4\u8edf\u306a\u53cd\u9762\u3001\u81ea\u5206\u3067\u884c\u3046\u4f5c\u696d\u304c\u5897\u3048\u308b\u306e\u3067\u5927\u5909\u3067\u3059\u3002<\/p>\n<h3>has_and_belongs_to_many\u3092\u4f7f\u3046\u65b9\u6cd5<\/h3>\n<p>\u5358\u7d14\u3067\u7c21\u6f54\u306b\u8a18\u8ff0\u304c\u3067\u304d\u308b\u3002<\/p>\n<h2>annnotate(\u4fbf\u5229\u306aGem)<\/h2>\n<p>\u30c6\u30fc\u30d6\u30eb\u306e\u69cb\u9020\u3092\u78ba\u8a8d\u3057\u305f\u3044\u3068\u304d\u306b\u308f\u3056\u308f\u3056DB\u306b\u63a5\u7d9a\u3057\u306a\u304f\u3066\u3082\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\u3092\u898b\u308c\u3070\u7406\u89e3\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u975e\u5e38\u306b\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">group :development do\r\n  # Access an IRB console on exception pages or by using &lt;%= console %&gt; anywhere in the code.\r\n  gem 'web-console'\r\n  gem 'listen', '~&gt; 3.0.5'\r\n  # Spring speeds up development by keeping your application running in the background. Read more: https:\/\/github.com\/rails\/spring\r\n  gem 'spring'\r\n  gem 'spring-watcher-listen', '~&gt; 2.0.0'\r\n  gem 'pry-byebug'\r\n  gem 'annotate' \u2605\u8ffd\u52a0\r\nend<\/pre>\n<p>\u65e2\u5b58\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u304c\u8ffd\u52a0\u3055\u308c\u308b\u3088\u3046\u306b\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true\">annotation<\/pre>\n<p>Docker\u3092\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408<\/p>\n<pre class=\"lang:default decode:true\">docker-compose exec web bundle exec annotate<\/pre>\n<p>\u3053\u308c\u3067\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\u306bDB\u69cb\u9020\u304c\u8ffd\u8a18\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\u306bDB\u69cb\u9020\u304c\u8ffd\u8a18\u3055\u308c\u306a\u3044\u5834\u5408<\/h3>\n<p>\u4e0b\u8a18\u3067annotate\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true\">rails g annotate:install<\/pre>\n<p>\u4f5c\u6210\u3055\u308c\u305f\u300c\u00a0lib\/tasks\/auto_annotate_models.rake \u300d\u3092\u958b\u304d\u4e0b\u8a18\u306e\u8a18\u8ff0\u3092true \u2192 false\u306b\u5909\u66f4\u3059\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \">'show_indexes'              =&gt; 'true',<\/pre>\n<p>\u518d\u5ea6\u300cannotation\u300d\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<h3>\u30c6\u30fc\u30d6\u30eb\u3092\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3067\u4f5c\u6210\u3059\u308b\u305f\u3073\u306bDB\u69cb\u9020\u304c\u8ffd\u8a18\u3055\u305b\u308b\u65b9\u6cd5<\/h3>\n<p>\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3060\u3051\u3067\u826f\u3044\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true  \">rails g annotate:install<\/pre>\n","protected":false},"excerpt":{"rendered":"\u3053\u3053\u3067\u3054\u7d39\u4ecb\u3059\u308b\u306e\u306f\u3042\u304f\u307e\u3067Rails\u3067\u30e2\u30c7\u30eb\u540c\u58eb\u306e\u95a2\u9023\u3065\u3051\u304c\u3042\u3063\u305f\u5834\u5408\u306b\u4fbf\u5229\u306b\u6271\u3046\u6a5f\u80fd\u306e\u3054\u7d39\u4ecb\u3067\u3042\u308a\u7d76\u5bfe\u306b\u95a2\u9023\u3065\u3051\u304c\u3042\u308b\u969b\u306b\u5b9a\u7fa9\u3057\u306a\u3051\u308c\u3070\u52d5\u304b\u306a\u3044\u3068\u3044\u3046\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u77e5\u3063\u3066\u304a\u304f\u3068\u975e\u5e38\u306b\u958b\u767a\u52b9\u7387\u304c\u4e0a\u304c\u308b\u6a5f\u80fd\u306a\u306e\u3067\u77e5 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[36],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7687"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7687"}],"version-history":[{"count":17,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7687\/revisions"}],"predecessor-version":[{"id":20537,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7687\/revisions\/20537"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7687"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}