{"id":1351,"date":"2025-03-24T08:52:18","date_gmt":"2025-03-23T23:52:18","guid":{"rendered":"https:\/\/dexall.co.jp\/articles\/?p=1351"},"modified":"2025-03-24T08:52:18","modified_gmt":"2025-03-23T23:52:18","slug":"%e3%80%90%e4%bf%9d%e5%ad%98%e7%89%88%e3%80%91rails-belongs_to%e3%82%a2%e3%82%bd%e3%82%b7%e3%82%a8%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e5%ae%8c%e5%85%a8%e3%82%ac%e3%82%a4%e3%83%89%ef%bc%9a7%e3%81%a4","status":"publish","type":"post","link":"https:\/\/dexall.co.jp\/articles\/?p=1351","title":{"rendered":"\u3010\u4fdd\u5b58\u7248\u3011Rails belongs_to\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u5b8c\u5168\u30ac\u30a4\u30c9\uff1a7\u3064\u306e\u5b9f\u88c5\u30d1\u30bf\u30fc\u30f3\u3068\u8a2d\u8a08\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"i-0\">belongs_to\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306e\u57fa\u790e\u77e5\u8b58<\/h1>\n\n\n\n<div class=\"toc\"><br \/>\n<b>Warning<\/b>:  Undefined array key \"is_admin\" in <b>\/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>116<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_category_top\" in <b>\/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>121<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_top\" in <b>\/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>128<\/b><br \/>\n    <div id=\"toc_container\" class=\"sgb-toc--bullets js-smooth-scroll\" data-dialog-title=\"\u76ee\u6b21\">\n      <p class=\"toc_title\">\u76ee\u6b21 <\/p>\n      <ul class=\"toc_list\">  <li class=\"first\">    <a href=\"#i-0\">belongs_to\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306e\u57fa\u790e\u77e5\u8b58<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-1\">belongs_to\u304c\u89e3\u6c7a\u3059\u308b3\u3064\u306e\u958b\u767a\u8ab2\u984c<\/a>      <\/li>      <li>        <a href=\"#i-2\">has_many\u3068belongs_to\u306e\u9055\u3044\u3092\u7406\u89e3\u3059\u308b<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-3\">\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u304c\u5185\u90e8\u3067\u884c\u3063\u3066\u3044\u308b\u3053\u3068<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-4\">belongs_to\u306e\u5b9f\u88c5\u30d1\u30bf\u30fc\u30f37\u9078<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-5\">\u30b7\u30f3\u30d7\u30eb\u306a1\u5bfe\u591a\u306e\u95a2\u4fc2\u3092\u5b9f\u88c5\u3059\u308b<\/a>      <\/li>      <li>        <a href=\"#i-6\">\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\u95a2\u9023\u4ed8\u3051\u3067\u67d4\u8edf\u306a\u8a2d\u8a08\u3092\u5b9f\u73fe\u3059\u308b<\/a>      <\/li>      <li>        <a href=\"#i-7\">optional: true\u3067\u4efb\u610f\u306e\u95a2\u9023\u4ed8\u3051\u3092\u5b9f\u88c5\u3059\u308b<\/a>      <\/li>      <li>        <a href=\"#i-8\">foreign_key\u3067\u5916\u90e8\u30ad\u30fc\u3092\u67d4\u8edf\u306b\u8a2d\u5b9a\u3059\u308b<\/a>      <\/li>      <li>        <a href=\"#i-9\">class_name\u3067\u7570\u306a\u308b\u30e2\u30c7\u30eb\u540d\u3092\u6307\u5b9a\u3059\u308b<\/a>      <\/li>      <li>        <a href=\"#i-10\">dependent: :destroy\u3067\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u3092\u4fdd\u3064<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-11\">\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3068\u7d44\u307f\u5408\u308f\u305b\u3066\u5805\u7262\u6027\u3092\u9ad8\u3081\u308b<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-12\">belongs_to\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-13\">N+1\u554f\u984c\u3092\u56de\u907f\u3059\u308b\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u6226\u7565<\/a>      <\/li>      <li>        <a href=\"#i-16\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u6d3b\u7528\u3057\u305f\u30af\u30a8\u30ea\u306e\u6700\u9069\u5316<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-17\">\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u6d3b\u7528\u3057\u305f\u9ad8\u901f\u5316\u30c6\u30af\u30cb\u30c3\u30af<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-19\">\u5b9f\u8df5\u7684\u306abelongs_to\u6d3b\u7528\u8853<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-20\">\u30e2\u30b8\u30e5\u30fc\u30eb\u5316\u306b\u3088\u308b\u518d\u5229\u7528\u53ef\u80fd\u306a\u95a2\u9023\u4ed8\u3051\u306e\u5b9f\u88c5<\/a>      <\/li>      <li>        <a href=\"#i-21\">\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3067\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306e\u52d5\u4f5c\u3092\u62c5\u4fdd\u3059\u308b<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-22\">\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/a>      <\/li>    <\/ul>  <\/li>  <li class=\"last\">    <a href=\"#i-23\">belongs_to\u306e\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-24\">\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u30a8\u30e9\u30fc\u306e\u89e3\u6c7a\u65b9\u6cd5<\/a>      <\/li>      <li>        <a href=\"#i-27\">\u5faa\u74b0\u53c2\u7167\u3092\u9632\u3050\u8a2d\u8a08\u30a2\u30d7\u30ed\u30fc\u30c1<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-28\">\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\u95a2\u9023\u4ed8\u3051\u3067\u306e\u6ce8\u610f\u70b9<\/a>      <\/li>    <\/ul>  <\/li><\/ul>\n      <a href=\"#\" class=\"sgb-toc-button js-toc-button\" rel=\"nofollow\" data-open-dialog=\"true\"><i class=\"fa fa-list\"><\/i><span class=\"sgb-toc-button__text\">\u76ee\u6b21\u3078<\/span><\/a>\n    <\/div><\/div><h2 class=\"wp-block-heading\" id=\"i-1\">belongs_to\u304c\u89e3\u6c7a\u3059\u308b3\u3064\u306e\u958b\u767a\u8ab2\u984c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Rails\u306ebelongs_to\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306f\u3001\u4ee5\u4e0b\u306e3\u3064\u306e\u958b\u767a\u8ab2\u984c\u3092\u52b9\u679c\u7684\u306b\u89e3\u6c7a\u3057\u307e\u3059\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30c7\u30fc\u30bf\u306e\u95a2\u9023\u4ed8\u3051\u306e\u7c21\u7d20\u5316<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5916\u90e8\u30ad\u30fc\u3092\u4f7f\u7528\u3057\u305f\u30c7\u30fc\u30bf\u306e\u95a2\u9023\u4ed8\u3051\u3092\u76f4\u611f\u7684\u306b\u8a18\u8ff0\u53ef\u80fd<\/li>\n\n\n\n<li>\u8907\u96d1\u306aJOIN\u30af\u30a8\u30ea\u3092\u30e2\u30c7\u30eb\u5c64\u3067\u30ab\u30d7\u30bb\u30eb\u5316<\/li>\n\n\n\n<li>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u306e\u7ba1\u7406\u3092\u5bb9\u6613\u306b<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u306e\u78ba\u4fdd<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53c2\u7167\u6574\u5408\u6027\u306e\u81ea\u52d5\u30c1\u30a7\u30c3\u30af<\/li>\n\n\n\n<li>\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3068\u306e\u9023\u643a\u306b\u3088\u308b\u4e0d\u6b63\u306a\u30c7\u30fc\u30bf\u306e\u9632\u6b62<\/li>\n\n\n\n<li>\u95a2\u9023\u30ec\u30b3\u30fc\u30c9\u306e\u524a\u9664\u6642\u306e\u6574\u5408\u6027\u7dad\u6301<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027\u306e\u5411\u4e0a<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u306e\u610f\u56f3\u3092\u660e\u78ba\u306b\u8868\u73fe<\/li>\n\n\n\n<li>DRY\u306a\u5b9f\u88c5\u306b\u3088\u308b\u91cd\u8907\u30b3\u30fc\u30c9\u306e\u524a\u6e1b<\/li>\n\n\n\n<li>\u30e2\u30c7\u30eb\u9593\u306e\u4f9d\u5b58\u95a2\u4fc2\u306e\u660e\u793a\u7684\u306a\u8868\u73fe<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-2\">has_many\u3068belongs_to\u306e\u9055\u3044\u3092\u7406\u89e3\u3059\u308b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">belongs_to\u3068has_many\u306f\u30011\u5bfe\u591a\u306e\u95a2\u4fc2\u3092\u8868\u73fe\u3059\u308b\u969b\u306e\u76f8\u4e92\u88dc\u5b8c\u7684\u306a\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># belongs_to\u306e\u4f8b\uff08\u5b50\u30e2\u30c7\u30eb\u5074\uff09\nclass Comment &lt; ApplicationRecord\n  belongs_to :post  # \u5358\u6570\u5f62\u3067\u6307\u5b9a\nend\n\n# has_many\u306e\u4f8b\uff08\u89aa\u30e2\u30c7\u30eb\u5074\uff09\nclass Post &lt; ApplicationRecord\n  has_many :comments  # \u8907\u6570\u5f62\u3067\u6307\u5b9a\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3b\u306a\u9055\u3044\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a<\/p>\n\n\n<div id=\"id-75acc48c-290e-4fa3-9a91-0ac23495f940\">\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u5fb4<\/th><th>belongs_to<\/th><th>has_many<\/th><\/tr><\/thead><tbody><tr><td>\u95a2\u9023\u306e\u65b9\u5411<\/td><td>\u5b50\u304b\u3089\u89aa\u3092\u53c2\u7167<\/td><td>\u89aa\u304b\u3089\u5b50\u3092\u53c2\u7167<\/td><\/tr><tr><td>\u5916\u90e8\u30ad\u30fc\u306e\u4f4d\u7f6e<\/td><td>\u81ea\u8eab\u306e\u30c6\u30fc\u30d6\u30eb<\/td><td>\u76f8\u624b\u306e\u30c6\u30fc\u30d6\u30eb<\/td><\/tr><tr><td>\u547d\u540d\u898f\u5247<\/td><td>\u5358\u6570\u5f62<\/td><td>\u8907\u6570\u5f62<\/td><\/tr><tr><td>\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u5fc5\u9808\u6027<\/td><td>Rails 5\u4ee5\u964d\u306f\u5fc5\u9808<\/td><td>\u4efb\u610f<\/td><\/tr><tr><td>\u4e3b\u306a\u7528\u9014<\/td><td>\u5f93\u5c5e\u95a2\u4fc2\u306e\u8868\u73fe<\/td><td>\u6240\u6709\u95a2\u4fc2\u306e\u8868\u73fe<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"i-3\">\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u304c\u5185\u90e8\u3067\u884c\u3063\u3066\u3044\u308b\u3053\u3068<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">belongs_to\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u304c\u5185\u90e8\u3067\u5b9f\u884c\u3057\u3066\u3044\u308b\u4e3b\u306a\u51e6\u7406\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30e1\u30bf\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306b\u3088\u308b\u30e1\u30bd\u30c3\u30c9\u5b9a\u7fa9<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Comment &lt; ApplicationRecord\n  belongs_to :post\n  # \u4ee5\u4e0b\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u81ea\u52d5\u7684\u306b\u5b9a\u7fa9\u3055\u308c\u308b\n  # post          # \u95a2\u9023\u4ed8\u3051\u3089\u308c\u305fPost\u3092\u53d6\u5f97\n  # post=         # \u95a2\u9023\u4ed8\u3051\u3092\u8a2d\u5b9a\n  # build_post    # \u65b0\u3057\u3044\u95a2\u9023\u4ed8\u3051\u3092\u69cb\u7bc9\n  # create_post   # \u65b0\u3057\u3044\u95a2\u9023\u4ed8\u3051\u3092\u4f5c\u6210\u3057\u3066\u4fdd\u5b58\n  # reload_post   # \u95a2\u9023\u4ed8\u3051\u3092\u518d\u8aad\u307f\u8fbc\u307f\nend<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u5916\u90e8\u30ad\u30fc\u306e\u81ea\u52d5\u7ba1\u7406<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># comment\u304c\u4f5c\u6210\u3055\u308c\u308b\u969b\u306b\u3001\u81ea\u52d5\u7684\u306bpost_id\u304c\u8a2d\u5b9a\u3055\u308c\u308b\ncomment = Comment.create(post: post)\ncomment.post_id  # =&gt; post\u306eid\u5024\u304c\u81ea\u52d5\u7684\u306b\u8a2d\u5b9a\u3055\u308c\u308b<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u7ba1\u7406<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">comment = Comment.first\ncomment.post    # \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089post\u3092\u53d6\u5f97\ncomment.post    # 2\u56de\u76ee\u4ee5\u964d\u306f\u30ad\u30e3\u30c3\u30b7\u30e5\u304b\u3089\u53d6\u5f97\uff08\u30af\u30a8\u30ea\u306f\u767a\u884c\u3055\u308c\u306a\u3044\uff09\ncomment.reload_post  # \u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u7121\u8996\u3057\u3066\u518d\u53d6\u5f97<\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306e\u5b9f\u884c<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Comment &lt; ApplicationRecord\n  belongs_to :post  # Rails 5\u4ee5\u964d\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067presence: true\nend\n\ncomment = Comment.new\ncomment.valid?  # =&gt; false\ncomment.errors.messages  # =&gt; { post: [\"must exist\"] }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u3088\u3046\u306b\u3001belongs_to\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306f\u5358\u306a\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u95a2\u9023\u4ed8\u3051\u3092\u8d85\u3048\u3066\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u7684\u306a\u95a2\u4fc2\u6027\u306e\u8868\u73fe\u3068\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u7ba1\u7406\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u3059\u3002\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u57fa\u790e\u77e5\u8b58\u3092\u6d3b\u7528\u3057\u305f\u5177\u4f53\u7684\u306a\u5b9f\u88c5\u30d1\u30bf\u30fc\u30f3\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"i-4\">belongs_to\u306e\u5b9f\u88c5\u30d1\u30bf\u30fc\u30f37\u9078<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-5\">\u30b7\u30f3\u30d7\u30eb\u306a1\u5bfe\u591a\u306e\u95a2\u4fc2\u3092\u5b9f\u88c5\u3059\u308b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u3082\u57fa\u672c\u7684\u306a\u5b9f\u88c5\u30d1\u30bf\u30fc\u30f3\u306f\u30011\u5bfe\u591a\u306e\u95a2\u4fc2\u3067\u3059\u3002\u4ee5\u4e0b\u306f\u8a18\u4e8b\u3068\u30ab\u30c6\u30b4\u30ea\u306e\u95a2\u4fc2\u3092\u4f8b\u306b\u8aac\u660e\u3057\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30d5\u30a1\u30a4\u30eb\nclass CreateArticlesAndCategories &lt; ActiveRecord::Migration[7.0]\n  def change\n    create_table :categories do |t|\n      t.string :name\n      t.timestamps\n    end\n\n    create_table :articles do |t|\n      t.string :title\n      t.text :content\n      t.references :category, foreign_key: true\n      t.timestamps\n    end\n  end\nend\n\n# \u30e2\u30c7\u30eb\u5b9a\u7fa9\nclass Article &lt; ApplicationRecord\n  belongs_to :category\nend\n\nclass Category &lt; ApplicationRecord\n  has_many :articles\nend<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-6\">\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\u95a2\u9023\u4ed8\u3051\u3067\u67d4\u8edf\u306a\u8a2d\u8a08\u3092\u5b9f\u73fe\u3059\u308b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8907\u6570\u306e\u30e2\u30c7\u30eb\u306b\u5bfe\u3057\u3066\u540c\u3058\u3088\u3046\u306a\u95a2\u9023\u4ed8\u3051\u304c\u5fc5\u8981\u306a\u5834\u5408\u3001\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\u95a2\u9023\u4ed8\u3051\u304c\u6709\u52b9\u3067\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30d5\u30a1\u30a4\u30eb\nclass CreateCommentsAndAttachments &lt; ActiveRecord::Migration[7.0]\n  def change\n    create_table :comments do |t|\n      t.text :content\n      t.references :commentable, polymorphic: true\n      t.timestamps\n    end\n\n    create_table :attachments do |t|\n      t.string :file_name\n      t.references :attachable, polymorphic: true\n      t.timestamps\n    end\n  end\nend\n\n# \u30e2\u30c7\u30eb\u5b9a\u7fa9\nclass Comment &lt; ApplicationRecord\n  belongs_to :commentable, polymorphic: true\nend\n\nclass Attachment &lt; ApplicationRecord\n  belongs_to :attachable, polymorphic: true\nend\n\nclass Article &lt; ApplicationRecord\n  has_many :comments, as: :commentable\n  has_many :attachments, as: :attachable\nend\n\nclass Product &lt; ApplicationRecord\n  has_many :comments, as: :commentable\n  has_many :attachments, as: :attachable\nend<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-7\">optional: true\u3067\u4efb\u610f\u306e\u95a2\u9023\u4ed8\u3051\u3092\u5b9f\u88c5\u3059\u308b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Rails 5\u4ee5\u964d\u3067\u306f\u3001belongs_to\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u5fc5\u9808\u306b\u306a\u308a\u307e\u3057\u305f\u304c\u3001optional: true\u3067\u4efb\u610f\u306e\u95a2\u9023\u4ed8\u3051\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Draft &lt; ApplicationRecord\n  belongs_to :editor, optional: true\n  # editor\u306e\u5b58\u5728\u30c1\u30a7\u30c3\u30af\u3092\u884c\u308f\u306a\u3044\nend\n\n# \u4f7f\u7528\u4f8b\ndraft = Draft.new(title: \"\u4e0b\u66f8\u304d\")\ndraft.valid?  # =&gt; true\uff08editor\u304c\u672a\u8a2d\u5b9a\u3067\u3082\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u304c\u901a\u308b\uff09<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-8\">foreign_key\u3067\u5916\u90e8\u30ad\u30fc\u3092\u67d4\u8edf\u306b\u8a2d\u5b9a\u3059\u308b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u547d\u540d\u898f\u5247\u3068\u7570\u306a\u308b\u5916\u90e8\u30ad\u30fc\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Employee &lt; ApplicationRecord\n  belongs_to :manager, \n             class_name: 'Employee',\n             foreign_key: 'manager_employee_id'\nend\n\n# \u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u4f8b\nclass CreateEmployees &lt; ActiveRecord::Migration[7.0]\n  def change\n    create_table :employees do |t|\n      t.string :name\n      t.integer :manager_employee_id\n      t.timestamps\n    end\n  end\nend<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-9\">class_name\u3067\u7570\u306a\u308b\u30e2\u30c7\u30eb\u540d\u3092\u6307\u5b9a\u3059\u308b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u30e2\u30c7\u30eb\u540d\u3068\u95a2\u9023\u4ed8\u3051\u540d\u304c\u7570\u306a\u308b\u5834\u5408\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Post &lt; ApplicationRecord\n  belongs_to :author, class_name: 'User'\n  belongs_to :reviewer, class_name: 'User'\nend\n\n# \u4f7f\u7528\u4f8b\npost = Post.new\npost.build_author(name: \"\u57f7\u7b46\u8005\")\npost.build_reviewer(name: \"\u30ec\u30d3\u30e5\u30a2\u30fc\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-10\">dependent: :destroy\u3067\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u3092\u4fdd\u3064<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u95a2\u9023\u30ec\u30b3\u30fc\u30c9\u306e\u524a\u9664\u6642\u306e\u632f\u308b\u821e\u3044\u3092\u5236\u5fa1\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Profile &lt; ApplicationRecord\n  belongs_to :user, dependent: :destroy\n  # user\u304c\u524a\u9664\u3055\u308c\u305f\u6642\u306b\u30d7\u30ed\u30d5\u30a3\u30fc\u30eb\u3082\u524a\u9664\u3055\u308c\u308b\nend\n\n# \u9006\u306e\u95a2\u4fc2\u306e\u5834\u5408\nclass User &lt; ApplicationRecord\n  has_one :profile, dependent: :destroy\n  # \u30e6\u30fc\u30b6\u30fc\u524a\u9664\u6642\u306b\u30d7\u30ed\u30d5\u30a3\u30fc\u30eb\u3082\u524a\u9664\nend<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-11\">\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3068\u7d44\u307f\u5408\u308f\u305b\u3066\u5805\u7262\u6027\u3092\u9ad8\u3081\u308b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u30ab\u30b9\u30bf\u30e0\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3068\u306e\u7d44\u307f\u5408\u308f\u305b\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Comment &lt; ApplicationRecord\n  belongs_to :post\n  belongs_to :user\n\n  validate :user_can_comment_on_post\n\n  private\n\n  def user_can_comment_on_post\n    return unless user &amp;&amp; post\n\n    unless user.can_comment_on?(post)\n      errors.add(:base, \"\u3053\u306e\u30e6\u30fc\u30b6\u30fc\u306f\u6295\u7a3f\u306b\u30b3\u30e1\u30f3\u30c8\u3067\u304d\u307e\u305b\u3093\")\n    end\n  end\nend\n\n# \u4f7f\u7528\u4f8b\nclass User &lt; ApplicationRecord\n  def can_comment_on?(post)\n    # \u30b3\u30e1\u30f3\u30c8\u6a29\u9650\u30c1\u30a7\u30c3\u30af\u306e\u30ed\u30b8\u30c3\u30af\n    active? &amp;&amp; !banned? &amp;&amp; (post.public? || subscribed_to?(post))\n  end\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u5b9f\u88c5\u30d1\u30bf\u30fc\u30f3\u306f\u3001\u72b6\u6cc1\u306b\u5fdc\u3058\u3066\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u67d4\u8edf\u3067\u5805\u7262\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u8a2d\u8a08\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u5b9f\u88c5\u30d1\u30bf\u30fc\u30f3\u3092\u4f7f\u7528\u3059\u308b\u969b\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"i-12\">belongs_to\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-13\">N+1\u554f\u984c\u3092\u56de\u907f\u3059\u308b\u30a4\u30f3\u30af\u30eb\u30fc\u30c9\u6226\u7565<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">belongs_to\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u3067\u3088\u304f\u767a\u751f\u3059\u308bN+1\u554f\u984c\u306e\u89e3\u6c7a\u65b9\u6cd5\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-14\">N+1\u554f\u984c\u306e\u4f8b\u3068\u89e3\u6c7a\u7b56<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># N+1\u554f\u984c\u304c\u767a\u751f\u3059\u308b\u30b3\u30fc\u30c9\nclass CommentsController &lt; ApplicationController\n  def index\n    @comments = Comment.all\n    # \u5404\u30b3\u30e1\u30f3\u30c8\u306b\u5bfe\u3057\u3066\u30e6\u30fc\u30b6\u30fc\u53d6\u5f97\u306e\u30af\u30a8\u30ea\u304c\u767a\u884c\u3055\u308c\u308b\n    @comments.each do |comment|\n      puts comment.user.name  # N\u56de\u306e\u30af\u30a8\u30ea\u304c\u767a\u751f\n    end\n  end\nend\n\n# includes \u3092\u4f7f\u7528\u3057\u305f\u89e3\u6c7a\u7b56\nclass CommentsController &lt; ApplicationController\n  def index\n    @comments = Comment.includes(:user)\n    # 1\u56de\u306e\u30af\u30a8\u30ea\u3067\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u3082\u53d6\u5f97\u6e08\u307f\n    @comments.each do |comment|\n      puts comment.user.name  # \u30af\u30a8\u30ea\u306f\u767a\u884c\u3055\u308c\u306a\u3044\n    end\n  end\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-15\">\u8907\u6570\u306e\u95a2\u9023\u3092\u542b\u3080\u5834\u5408<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u8907\u6570\u306e\u95a2\u9023\u3092\u540c\u6642\u306b\u30d7\u30ea\u30ed\u30fc\u30c9\n@comments = Comment.includes(:user, :post, post: :category)\n\n# \u30cd\u30b9\u30c8\u3055\u308c\u305f\u95a2\u9023\u306e\u30d7\u30ea\u30ed\u30fc\u30c9\nclass Comment &lt; ApplicationRecord\n  belongs_to :post\n  belongs_to :user\n  has_many :reactions\nend\n\n# \u8907\u96d1\u306a\u95a2\u9023\u306e\u30d7\u30ea\u30ed\u30fc\u30c9\u4f8b\n@comments = Comment.includes(\n  :user,\n  post: [:category, :tags],\n  reactions: :user\n)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-16\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u6d3b\u7528\u3057\u305f\u30af\u30a8\u30ea\u306e\u6700\u9069\u5316<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">belongs_to\u306e\u5916\u90e8\u30ad\u30fc\u306b\u306f\u9069\u5207\u306a\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3067\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u8a2d\u5b9a\nclass AddIndicesToComments &lt; ActiveRecord::Migration[7.0]\n  def change\n    # \u5358\u4e00\u30ab\u30e9\u30e0\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\n    add_index :comments, :user_id\n\n    # \u8907\u5408\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\n    add_index :comments, [:post_id, :created_at]\n\n    # \u30e6\u30cb\u30fc\u30af\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\n    add_index :profiles, :user_id, unique: true\n  end\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u8a2d\u8a08\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\uff1a<\/p>\n\n\n<div id=\"id-476e55a7-e9fb-4dec-94aa-f8191b306d3d\">\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30bf\u30a4\u30d7<\/th><th>\u4f7f\u7528\u30b1\u30fc\u30b9<\/th><th>\u30e1\u30ea\u30c3\u30c8<\/th><\/tr><\/thead><tbody><tr><td>\u5358\u4e00\u30ab\u30e9\u30e0<\/td><td>\u57fa\u672c\u7684\u306a\u5916\u90e8\u30ad\u30fc\u53c2\u7167<\/td><td>\u901a\u5e38\u306e\u691c\u7d22\u304c\u9ad8\u901f\u5316<\/td><\/tr><tr><td>\u8907\u5408\u30a4\u30f3\u30c7\u30c3\u30af\u30b9<\/td><td>\u8907\u6570\u6761\u4ef6\u3067\u306e\u691c\u7d22<\/td><td>\u8907\u96d1\u306a\u30af\u30a8\u30ea\u304c\u52b9\u7387\u5316<\/td><\/tr><tr><td>\u30e6\u30cb\u30fc\u30af<\/td><td>1\u5bfe1\u306e\u95a2\u4fc2<\/td><td>\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u78ba\u4fdd<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"i-17\">\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u6d3b\u7528\u3057\u305f\u9ad8\u901f\u5316\u30c6\u30af\u30cb\u30c3\u30af<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">belongs_to\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u3067\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u6d3b\u7528\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Comment &lt; ApplicationRecord\n  belongs_to :post, touch: true\n  # post\u304c\u66f4\u65b0\u3055\u308c\u308b\u3068\u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u304c\u66f4\u65b0\u3055\u308c\u308b\n\n  belongs_to :user\n\n  # \u30ad\u30e3\u30c3\u30b7\u30e5\u30ad\u30fc\u306e\u8a2d\u5b9a\n  def cache_key\n    \"comment\/#{id}-#{updated_at}\"\n  end\nend\n\n# \u30d3\u30e5\u30fc\u3067\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u4f7f\u7528\u4f8b\n&lt;% cache @comment do %&gt;\n  &lt;div class=\"comment\"&gt;\n    &lt;% cache @comment.user do %&gt;\n      &lt;%= render 'users\/avatar', user: @comment.user %&gt;\n    &lt;% end %&gt;\n    &lt;%= @comment.content %&gt;\n  &lt;\/div&gt;\n&lt;% end %&gt;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-18\">\u30ad\u30e3\u30c3\u30b7\u30e5\u6226\u7565\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30ed\u30b7\u30a2\u30f3\u30c9\u30fc\u30eb\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u6d3b\u7528<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># application_helper.rb\ndef cache_nested(object, &amp;block)\n  cache([object, 'v1'], &amp;block)\nend\n\n# \u30d3\u30e5\u30fc\u3067\u306e\u4f7f\u7528\n&lt;% cache_nested @post do %&gt;\n  &lt;article&gt;\n    &lt;% cache_nested @post.user do %&gt;\n      &lt;%= render 'users\/info', user: @post.user %&gt;\n    &lt;% end %&gt;\n    &lt;%= @post.content %&gt;\n  &lt;\/article&gt;\n&lt;% end %&gt;<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u6761\u4ef6\u4ed8\u304d\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u5b9f\u88c5<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Comment &lt; ApplicationRecord\n  belongs_to :post\n\n  def cache_if_production(&amp;block)\n    if Rails.env.production?\n      Rails.cache.fetch(cache_key, &amp;block)\n    else\n      yield\n    end\n  end\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u624b\u6cd5\u3092\u9069\u5207\u306b\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001belongs_to\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u30bf\u30a4\u30e0\u3092\u5927\u5e45\u306b\u6539\u5584\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"i-19\">\u5b9f\u8df5\u7684\u306abelongs_to\u6d3b\u7528\u8853<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-20\">\u30e2\u30b8\u30e5\u30fc\u30eb\u5316\u306b\u3088\u308b\u518d\u5229\u7528\u53ef\u80fd\u306a\u95a2\u9023\u4ed8\u3051\u306e\u5b9f\u88c5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u95a2\u9023\u4ed8\u3051\u306e\u30ed\u30b8\u30c3\u30af\u3092\u518d\u5229\u7528\u53ef\u80fd\u306a\u5f62\u3067\u30e2\u30b8\u30e5\u30fc\u30eb\u5316\u3059\u308b\u65b9\u6cd5\u3092\u89e3\u8aac\u3057\u307e\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># app\/models\/concerns\/commentable.rb\nmodule Commentable\n  extend ActiveSupport::Concern\n\n  included do\n    has_many :comments, as: :commentable\n\n    def recent_comments(limit = 5)\n      comments.order(created_at: :desc).limit(limit)\n    end\n  end\nend\n\n# app\/models\/concerns\/with_user.rb\nmodule WithUser\n  extend ActiveSupport::Concern\n\n  included do\n    belongs_to :user\n\n    validates :user, presence: true\n\n    scope :by_user, -&gt;(user) { where(user: user) }\n  end\n\n  def authored_by?(user)\n    self.user_id == user&amp;.id\n  end\nend\n\n# \u5b9f\u88c5\u4f8b\nclass Post &lt; ApplicationRecord\n  include Commentable\n  include WithUser\nend\n\nclass Photo &lt; ApplicationRecord\n  include Commentable\n  include WithUser\nend<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-21\">\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3067\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306e\u52d5\u4f5c\u3092\u62c5\u4fdd\u3059\u308b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">RSpec\u3092\u4f7f\u7528\u3057\u305f\u30c6\u30b9\u30c8\u5b9f\u88c5\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># spec\/models\/comment_spec.rb\nRSpec.describe Comment, type: :model do\n  describe 'associations' do\n    it { should belong_to(:post) }\n    it { should belong_to(:user) }\n  end\n\n  describe 'validations' do\n    it { should validate_presence_of(:content) }\n  end\n\n  describe 'scopes' do\n    let(:user) { create(:user) }\n    let(:post) { create(:post) }\n\n    before do\n      create_list(:comment, 3, user: user, post: post)\n    end\n\n    it 'returns comments by user' do\n      expect(Comment.by_user(user).count).to eq(3)\n    end\n  end\n\n  describe 'callbacks' do\n    let(:comment) { build(:comment) }\n\n    it 'notifies post author after creation' do\n      expect(comment).to receive(:notify_post_author)\n      comment.save\n    end\n  end\nend\n\n# spec\/support\/shared_examples\/belongs_to_user.rb\nRSpec.shared_examples 'belongs_to_user' do\n  let(:model) { described_class }\n\n  it { is_expected.to belong_to(:user) }\n\n  describe '#authored_by?' do\n    let(:user) { create(:user) }\n    let(:instance) { create(model.to_s.underscore.to_sym, user: user) }\n\n    it 'returns true for the author' do\n      expect(instance.authored_by?(user)).to be true\n    end\n\n    it 'returns false for other users' do\n      expect(instance.authored_by?(create(:user))).to be false\n    end\n  end\nend<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-22\">\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u306e\u9069\u5207\u306a\u8a2d\u5b9a<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class CreateComments &lt; ActiveRecord::Migration[7.0]\n  def change\n    create_table :comments do |t|\n      t.references :post, null: false, \n                         foreign_key: { on_delete: :cascade }\n      t.references :user, null: false, \n                         foreign_key: { on_delete: :restrict }\n      t.text :content\n      t.timestamps\n    end\n  end\nend<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u65e2\u5b58\u30c6\u30fc\u30d6\u30eb\u3078\u306e\u95a2\u9023\u4ed8\u3051\u8ffd\u52a0<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class AddAuthorToBooks &lt; ActiveRecord::Migration[7.0]\n  def up\n    add_reference :books, :author, null: true\n    # \u65e2\u5b58\u30ec\u30b3\u30fc\u30c9\u3078\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u8a2d\u5b9a\n    Book.find_each do |book|\n      book.update_column(:author_id, book.legacy_author_id)\n    end\n    # null: false\u306e\u5236\u7d04\u3092\u8ffd\u52a0\n    change_column_null :books, :author_id, false\n    add_foreign_key :books, :authors\n  end\n\n  def down\n    remove_reference :books, :author\n  end\nend<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u6226\u7565<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class AddIndicesForPerformance &lt; ActiveRecord::Migration[7.0]\n  def change\n    # \u8907\u5408\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u8ffd\u52a0\n    add_index :comments, [:post_id, :user_id]\n    add_index :comments, [:post_id, :created_at]\n\n    # \u90e8\u5206\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u8ffd\u52a0\n    add_index :comments, :post_id, \n              where: \"deleted_at IS NULL\"\n\n    # \u30e6\u30cb\u30fc\u30af\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u8ffd\u52a0\n    add_index :profiles, [:user_id], \n              unique: true\n  end\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u5b9f\u8df5\u7684\u306a\u30c6\u30af\u30cb\u30c3\u30af\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u6027\u304c\u9ad8\u304f\u3001\u5805\u7262\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"i-23\">belongs_to\u306e\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-24\">\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u30a8\u30e9\u30fc\u306e\u89e3\u6c7a\u65b9\u6cd5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u306b\u95a2\u9023\u3059\u308b\u4e00\u822c\u7684\u306a\u554f\u984c\u3068\u305d\u306e\u89e3\u6c7a\u65b9\u6cd5\u3092\u89e3\u8aac\u3057\u307e\u3059\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-25\">1. \u524a\u9664\u6642\u306e\u5236\u7d04\u30a8\u30e9\u30fc<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u30a8\u30e9\u30fc\u306e\u4f8b\nActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  update or delete on table \"users\" violates foreign key constraint \"fk_rails_...\" on table \"comments\"\n\n# \u89e3\u6c7a\u7b561: dependent: :destroy \u306e\u8a2d\u5b9a\nclass User &lt; ApplicationRecord\n  has_many :comments, dependent: :destroy\nend\n\n# \u89e3\u6c7a\u7b562: \u5916\u90e8\u30ad\u30fc\u5236\u7d04\u306e\u5909\u66f4\nclass ChangeCommentsUserForeignKey &lt; ActiveRecord::Migration[7.0]\n  def change\n    remove_foreign_key :comments, :users\n    add_foreign_key :comments, :users, on_delete: :cascade\n  end\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-26\">2. NULL\u306e\u5236\u7d04\u30a8\u30e9\u30fc<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u30a8\u30e9\u30fc\u306e\u4f8b\nActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR:  null value in column \"user_id\" violates not-null constraint\n\n# \u89e3\u6c7a\u7b561: optional: true \u306e\u8a2d\u5b9a\nclass Comment &lt; ApplicationRecord\n  belongs_to :user, optional: true\nend\n\n# \u89e3\u6c7a\u7b562: \u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306e\u8a2d\u5b9a\nclass AddDefaultUserToComments &lt; ActiveRecord::Migration[7.0]\n  def change\n    change_column_default :comments, :user_id, from: nil, to: 0\n    # \u30b7\u30b9\u30c6\u30e0\u30e6\u30fc\u30b6\u30fc\u306eID\u306a\u3069\u3092\u8a2d\u5b9a\n  end\nend<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-27\">\u5faa\u74b0\u53c2\u7167\u3092\u9632\u3050\u8a2d\u8a08\u30a2\u30d7\u30ed\u30fc\u30c1<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5faa\u74b0\u53c2\u7167\u306b\u3088\u308b\u554f\u984c\u3092\u9632\u3050\u305f\u3081\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u554f\u984c\u306e\u3042\u308b\u5b9f\u88c5\nclass Employee &lt; ApplicationRecord\n  belongs_to :manager, class_name: 'Employee'\n  belongs_to :department\nend\n\nclass Department &lt; ApplicationRecord\n  belongs_to :manager, class_name: 'Employee'\nend\n\n# \u6539\u5584\u3055\u308c\u305f\u5b9f\u88c5\nclass Employee &lt; ApplicationRecord\n  belongs_to :manager, class_name: 'Employee', optional: true\n  belongs_to :department\n\n  has_one :managed_department, \n          class_name: 'Department',\n          foreign_key: 'manager_id'\n\n  validate :no_circular_management\n\n  private\n\n  def no_circular_management\n    current = manager\n    seen = Set.new([id])\n\n    while current\n      if seen.include?(current.id)\n        errors.add(:manager, \"\u5faa\u74b0\u53c2\u7167\u304c\u691c\u51fa\u3055\u308c\u307e\u3057\u305f\")\n        break\n      end\n      seen.add(current.id)\n      current = current.manager\n    end\n  end\nend<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-28\">\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\u95a2\u9023\u4ed8\u3051\u3067\u306e\u6ce8\u610f\u70b9<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-29\">1. \u30bf\u30a4\u30d7\u306e\u4e0d\u6574\u5408<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u554f\u984c\u306e\u3042\u308b\u5b9f\u88c5\nclass Comment &lt; ApplicationRecord\n  belongs_to :commentable, polymorphic: true\nend\n\n# \u30bf\u30a4\u30d7\u306e\u4e0d\u6574\u5408\u3092\u9632\u3050\nclass Comment &lt; ApplicationRecord\n  belongs_to :commentable, polymorphic: true\n\n  validates :commentable_type, inclusion: { \n    in: ['Post', 'Article', 'Photo'],\n    message: '\u7121\u52b9\u306acommentable_type\u3067\u3059'\n  }\n\n  # \u30ab\u30b9\u30bf\u30e0\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\n  validate :validate_commentable_type\n\n  private\n\n  def validate_commentable_type\n    return unless commentable\n\n    unless commentable.class.name == commentable_type\n      errors.add(:commentable, \"\u30bf\u30a4\u30d7\u306e\u4e0d\u6574\u5408\u304c\u767a\u751f\u3057\u3066\u3044\u307e\u3059\")\n    end\n  end\nend<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-30\">2. \u4e0d\u9069\u5207\u306a\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u8a2d\u5b9a<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u52b9\u7387\u7684\u306a\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u8a2d\u5b9a\nclass AddPolymorphicIndices &lt; ActiveRecord::Migration[7.0]\n  def change\n    # \u8907\u5408\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u8ffd\u52a0\n    add_index :comments, \n              [:commentable_type, :commentable_id],\n              name: 'index_comments_on_commentable'\n\n    # \u5fc5\u8981\u306b\u5fdc\u3058\u3066\u90e8\u5206\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3082\u8ffd\u52a0\n    add_index :comments,\n              [:commentable_type, :commentable_id],\n              name: 'index_active_comments_on_commentable',\n              where: 'deleted_at IS NULL'\n  end\nend<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u306e\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u624b\u6cd5\u3092\u7406\u89e3\u3057\u3001\u9069\u5207\u306b\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u3001belongs_to\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306b\u95a2\u9023\u3059\u308b\u591a\u304f\u306e\u554f\u984c\u3092\u672a\u7136\u306b\u9632\u304e\u3001\u767a\u751f\u3057\u305f\u554f\u984c\u3082\u52b9\u7387\u7684\u306b\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>belongs_to\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306e\u57fa\u790e\u77e5\u8b58 Warning: Undefined array key &#8220;is_admin&#8221; in \/home\/xs392991\/dexall.co.jp\/public_html\/art &#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1351","post","type-post","status-publish","format-standard","category-ruby","nothumb"],"_links":{"self":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/1351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1351"}],"version-history":[{"count":1,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/1351\/revisions"}],"predecessor-version":[{"id":1352,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/1351\/revisions\/1352"}],"wp:attachment":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}