{"id":12401,"date":"2020-12-19T09:28:52","date_gmt":"2020-12-19T00:28:52","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=12401"},"modified":"2023-05-27T22:43:32","modified_gmt":"2023-05-27T13:43:32","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%80%8chas_many%e3%80%8d%e3%81%ae%e3%82%aa%e3%83%97%e3%82%b7%e3%83%a7%e3%83%b3","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=12401","title":{"rendered":"\u3010Ruby on Rails\u3011\u300c\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3(has_many)\u300d\u306e\u30aa\u30d7\u30b7\u30e7\u30f3"},"content":{"rendered":"<h2>\u305d\u3082\u305d\u3082has_many\u3068\u306f\uff1f<\/h2>\n<p>\u5bfe\u8c61\u3092\u8907\u6570\u6301\u3063\u3066\u3044\u308b\u5834\u5408\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<h2>\u69cb\u6587<\/h2>\n<pre class=\"lang:default decode:true\">class \u89aa\u30e2\u30c7\u30eb &lt; ActiveRecord::Base\r\n  has_many :\u95a2\u9023\u3059\u308b\u5b50\u30e2\u30c7\u30eb\uff08\u8907\u6570\u5f62\uff09\r\nend<\/pre>\n<h3>\u5b9f\u88c5<\/h3>\n<p>\u8907\u6570\u30e2\u30c7\u30eb\u304c\u8fd4\u308b\u3002\u300cActiveRecord Reration\u300d\uff08\u5b50\u30e2\u30c7\u30eb\u306e\u914d\u5217\u307f\u305f\u3044\u306a\u69cb\u9020\uff09\u306b\u306a\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true\">\u89aa\u30e2\u30c7\u30eb.find(1).\u5b50\u30e2\u30c7\u30eb\uff08\u8907\u6570\uff09\r\n<\/pre>\n<h2>has_many\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u306e\u6982\u8981<\/h2>\n<table>\n<tbody>\n<tr>\n<th>\u30aa\u30d7\u30b7\u30e7\u30f3<\/th>\n<th>\u8aac\u660e<\/th>\n<\/tr>\n<tr>\n<td>class_name<\/td>\n<td>1\u30e2\u30c7\u30eb\u306b\u5bfe\u3057\u30662\u3064\u4ee5\u4e0a\u306e\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u3092\u7d44\u3080\u5834\u5408\u306b\u4f7f\u7528\u3059\u308b\u3002source\u3068\u307b\u307c\u6a5f\u80fd\u306f\u540c\u3058\u3060\u304c\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u304c\u306a\u3044\u5834\u5408\u306f\u3053\u3061\u3089\u3092\u4f7f\u3046\u3002<span style=\"color: #ff0000;\"><strong>\u305f\u3060\u3001\u305d\u3082\u305d\u3082<\/strong><strong>\u30e2\u30c7\u30eb\u540d\u3068\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u4f7f\u7528\u3057\u3066\u3044\u308b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u540d\u304c\u7570\u306a\u308a\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u4e0a\u3067\u306e\u53ef\u8aad\u6027\u304c\u4e0b\u304c\u308b\u306e\u3067\u3042\u307e\u308a\u4f7f\u3046\u3053\u3068\u306f\u306a\u3044\u3002<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td>source<\/td>\n<td>\u300cclass_name\u300d\u3068\u307b\u307c\u6a5f\u80fd\u306f\u540c\u3058\u3002through\u3092\u4f7f\u3046\u3088\u3046\u306a\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u304c\u3042\u308b\u969b\u306b\u306f\u3053\u3061\u3089\u3092\u4f7f\u3046\u3002<span style=\"color: #ff0000;\"><strong>\u305f\u3060\u3001\u305d\u3082\u305d\u3082<\/strong><strong>\u30e2\u30c7\u30eb\u540d\u3068\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u4f7f\u7528\u3057\u3066\u3044\u308b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u540d\u304c\u7570\u306a\u308a\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u4e0a\u3067\u306e\u53ef\u8aad\u6027\u304c\u4e0b\u304c\u308b\u306e\u3067\u3042\u307e\u308a\u4f7f\u3046\u3053\u3068\u306f\u306a\u3044\u3002<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td>foreign_key<\/td>\n<td>\u53c2\u7167\u5148\uff08\u5225\u30c6\u30fc\u30d6\u30eb\uff09\u306e\u5916\u90e8\u30ad\u30fc\u306e\u30ab\u30e9\u30e0\u540d\u3092\u6307\u5b9a\u3059\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<td>primary_key<\/td>\n<td>\u53c2\u7167\u5143\uff08\u81ea\u5206\uff09\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u5916\u90e8\u30ad\u30fc\u306e\u30ab\u30e9\u30e0\u540d<\/td>\n<\/tr>\n<tr>\n<td>through<\/td>\n<td>\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u304c\u8907\u6570\u3042\u308b\u5834\u5408\u306b\u4f7f\u7528\u3059\u308b\u3002\u3069\u306e\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u7528\u3059\u308c\u3070\u826f\u3044\u304b\u7d4c\u8def\u3092\u6c7a\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002source\u3068\u4e00\u7dd2\u306b\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u591a\u3044\u3002<\/td>\n<\/tr>\n<tr>\n<td>as<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>polymorphic<\/td>\n<td>\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\u95a2\u9023\u304c\u4f7f\u3048\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<td>dependent<\/td>\n<td>\u524a\u9664\u6642\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002\u89aa\u30e2\u30c7\u30eb\u3092\u524a\u9664\u3059\u308b\u969b\u306b\u5b50\u30e2\u30c7\u30eb\u304c\u3069\u306e\u3088\u3046\u306b\u524a\u9664\u3055\u308c\u308b\u304b\u3092\u6c7a\u3081\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>inverse_of<\/td>\n<td>\u4e00\u3064\u306e\u89aa\u30e2\u30c7\u30eb\u306b\u8907\u6570\u306e\u5b50\u30e2\u30c7\u30eb\u304c\u95a2\u9023\u3065\u3051\u3089\u308c\u308b\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u8a73\u7d30<\/h2>\n<h3>source<\/h3>\n<p>\u300cclass_name\u300d\u3068\u307b\u307c\u6a5f\u80fd\u306f\u540c\u3058\u3002through\u3092\u4f7f\u3046\u3088\u3046\u306a\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u304c\u3042\u308b\u969b\u306b\u306f\u3053\u3061\u3089\u3092\u4f7f\u3046\u3002\u95a2\u9023\u5148\u30c6\u30fc\u30d6\u30eb\u540d\u3068\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u540d\u304c\u540c\u3058\u5834\u5408\u306fsource\u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u7701\u7565\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3002\u7570\u306a\u308b\u5834\u5408\u306f\u300csource: :\u30e2\u30c7\u30eb\u540d\u300d\u3068\u3044\u3046\u3088\u3046\u306b\u95a2\u9023\u5148\u30c6\u30fc\u30d6\u30eb\u540d\u3092\u8a18\u8ff0\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<span style=\"color: #ff0000;\"><strong>\u305f\u3060\u3001\u305d\u3082\u305d\u3082<\/strong><strong>\u30e2\u30c7\u30eb\u540d\u3068\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u4f7f\u7528\u3057\u3066\u3044\u308b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u540d\u304c\u7570\u306a\u308a\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u4e0a\u3067\u306e\u53ef\u8aad\u6027\u304c\u4e0b\u304c\u308b\u306e\u3067\u3042\u307e\u308a\u4f7f\u3046\u3053\u3068\u306f\u306a\u3044\u3002<\/strong><\/span><\/p>\n<h3>inverse_of<\/h3>\n<p>has_many\u3068\u4e00\u7dd2\u306b\u4f7f\u7528\u3059\u308b\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002<\/p>\n<p>Article\u30e2\u30c7\u30eb(\uff11)\u3068Comments\u30e2\u30c7\u30eb\uff08\u591a\uff09\u3092\u5225\u3005\u306e\u5909\u6570\u306b\u4ee3\u5165\u3057\u305f\u3068\u3057\u3066\u3082\u3001Comments\uff08\u591a\uff09\u306e\u65b9\u304b\u30891\u306e\u65b9\u3092\u53c2\u7167\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306a\u8a2d\u5b9a\u3067\u3059\u3002\u540c\u3058\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u540c\u4e00\u30e1\u30e2\u30ea\u4e0a\u306b\u683c\u7d0d\u3059\u308b\u305f\u3081\u306e\u3088\u3046\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">project = Project.first\r\ncatepro = project.category_projects.first\r\nirb(main):034:0&gt; project.title == catepro.project.title\r\n=&gt; true\r\nirb(main):035:0&gt; project.title = \"henko\"\r\n=&gt; \"henko\"\r\nirb(main):036:0&gt; project.title == catepro.project.title\r\n=&gt; true\u3000\u2190\u300c\u89aa\u5074\u306e\u5909\u6570\u300d\u306b\u5909\u66f4\u3092\u52a0\u3048\u305f\u306e\u306b\u300c\u5b50\u306e\u5909\u6570\u300d\u304b\u3089\u8fbf\u3063\u3066\u53c2\u7167\u304c\u3067\u304d\u308b\u3002<\/pre>\n<p>\u306a\u304a\u3001\u3053\u306e\u6a5f\u80fd\u3092\u5229\u7528\u3059\u308b\u305f\u3081\u306b\u306fRails4.0\u307e\u3067\u306f\u660e\u793a\u7684\u306b\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u3001Rails4.1\u4ee5\u964d\u304b\u3089\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3088\u3046\u3067\u3059\u3002<\/p>\n<h2>dependent\u30aa\u30d7\u30b7\u30e7\u30f3<\/h2>\n<p>\u524a\u9664\u6642\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u306b\u306a\u308a\u307e\u3059\u3002\u89aa\u30e2\u30c7\u30eb\u3092\u524a\u9664\u3059\u308b\u969b\u306b\u5b50\u30e2\u30c7\u30eb\u304c\u3069\u306e\u3088\u3046\u306b\u524a\u9664\u3055\u308c\u308b\u304b\u6c7a\u5b9a\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<th>\u30aa\u30d7\u30b7\u30e7\u30f3\u540d<\/th>\n<th>\u8aac\u660e<\/th>\n<\/tr>\n<tr>\n<td>destroy<\/td>\n<td>\u89aa\u30ec\u30b3\u30fc\u30c9\u3068\u4e00\u7dd2\u306b\u5b50\u30ec\u30b3\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u3002\u5b50\u30ec\u30b3\u30fc\u30c9\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3082\u5b9f\u884c\u3059\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<td>delete_all<\/td>\n<td>\u89aa\u30ec\u30b3\u30fc\u30c9\u3068\u4e00\u7dd2\u306b\u5b50\u30ec\u30b3\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u3002SQL\u76f4\u63a5\u5b9f\u884c\u3067\u76f4\u63a5DB\u304b\u3089\u524a\u9664\u3059\u308b\u306e\u3067\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306a\u3057\u3002<\/td>\n<\/tr>\n<tr>\n<td>nullify<\/td>\n<td>\u5b50\u30ec\u30b3\u30fc\u30c9\u306e\u5916\u90e8\u30ad\u30fc\u3092NULL\u66f4\u65b0\u3059\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<td>restrict_with_exception<\/td>\n<td>\u5b50\u30ec\u30b3\u30fc\u30c9\u304c\u3042\u308b\u5834\u5408\u306f\u300cActiveRecord::DeleteRestrictionError\u300d\u304c\u767a\u751f\u3059\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<td>restrict_with_error<\/td>\n<td>\u5b50\u30ec\u30b3\u30fc\u30c9\u304c\u3042\u308b\u5834\u5408\u306f\u524a\u9664\u304c\u5931\u6557\u3057\u3001\u89aa\u30ec\u30b3\u30fc\u30c9\u306b\u30a8\u30e9\u30fc\u60c5\u5831\u3092\u4ed8\u4e0e\u3059\u308b\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u3063\u305f\u5b9f\u88c5\uff08\u591a\uff1a\u591a\uff09<\/h2>\n<h3>\u4e21\u7aef\u306e\u30c6\u30fc\u30d6\u30eb<\/h3>\n<pre class=\"lang:default decode:true\">class \u30e2\u30c7\u30eb\u540d &lt; ActiveRecord::Base\r\n  has_many :\u95a2\u9023\u540d, through: :\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u540d\r\n  has_many :\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u540d #throuth\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3059\u308b\u969b\u306e\u304a\u307e\u3058\u306a\u3044\r\nend<\/pre>\n<h3>\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb<\/h3>\n<pre class=\"lang:default decode:true \">class \u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u540d &lt; ActiveRecord::Base\r\n    belongs_to :\u7aef\u30c6\u30fc\u30d6\u30ebA\r\n    belongs_to :\u7aef\u30c6\u30fc\u30d6\u30ebB\r\nend<\/pre>\n<h3>\u5b9f\u88c5<\/h3>\n<p>\u7aef\u30e2\u30c7\u30ebA\u306b\u6240\u5c5e\u3059\u308b\u7aef\u30e2\u30c7\u30ebB\u306e\u4e00\u89a7\u3092\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u53d6\u5f97\u3057\u3066\u3001\u7aef\u30e2\u30c7\u30ebB\u306b\u7d10\u3065\u304f\u4e00\u89a7\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u4e00\u62ec\u3067\u53d6\u5f97\u3059\u308b\u3068\u3044\u3046\u51e6\u7406\u3092\u4e0b\u8a18\u306e\u6587\u3067\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">\u7aef\u30e2\u30c7\u30ebA.find(1).\u7aef\u30e2\u30c7\u30ebB<\/pre>\n<h4>\u5185\u90e8\u7684\u306b\u751f\u6210\u3055\u308c\u308bSQL<\/h4>\n<pre class=\"lang:default decode:true  \">SELECT * FROM `\u7aef\u30c6\u30fc\u30d6\u30ebA`\r\n  INNER JOIN `\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb` \r\n    ON `\u7aef\u30c6\u30fc\u30d6\u30ebA`.`\u4e3b\u30ad\u30fc` = `\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb`.`\u7aef\u30c6\u30fc\u30d6\u30ebA\u3078\u306e\u5916\u90e8\u30ad\u30fc` \r\n      WHERE `\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb`.`\u7aef\u30c6\u30fc\u30d6\u30ebB\u3078\u306e\u5916\u90e8\u30ad\u30fc` = 1<\/pre>\n","protected":false},"excerpt":{"rendered":"\u305d\u3082\u305d\u3082has_many\u3068\u306f\uff1f \u5bfe\u8c61\u3092\u8907\u6570\u6301\u3063\u3066\u3044\u308b\u5834\u5408\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002 \u69cb\u6587 class \u89aa\u30e2\u30c7\u30eb &lt; ActiveRecord::Base has_many :\u95a2\u9023\u3059\u308b\u5b50\u30e2\u30c7\u30eb\uff08\u8907\u6570\u5f62\uff09 end \u5b9f\u88c5 \u8907\u6570\u30e2\u30c7\u30eb [&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\/12401"}],"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=12401"}],"version-history":[{"count":12,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12401\/revisions"}],"predecessor-version":[{"id":20497,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12401\/revisions\/20497"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12401"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}