{"id":1256,"date":"2020-05-31T20:40:41","date_gmt":"2020-05-31T11:40:41","guid":{"rendered":"http:\/\/168.138.214.208\/?p=1256"},"modified":"2020-05-31T20:40:41","modified_gmt":"2020-05-31T11:40:41","slug":"post-1256","status":"publish","type":"post","link":"https:\/\/nobunobu1717.site\/?p=1256","title":{"rendered":"\u591a\u5bfe\u591a\u306e\u95a2\u4fc2\u6027\u306e\u521d\u671f\u30c7\u30fc\u30bf\u306e\u4f5c\u6210\u65b9\u6cd5"},"content":{"rendered":"<h2>\u6982\u8981<\/h2>\n<p>Rails\u306eActiveRecord\u3067\u591a\u5bfe\u591a\u306e\u30c7\u30fc\u30bf\u3092\u5b9a\u7fa9\u3057\u305f\u6642\u306e\u30c7\u30fc\u30bf\u4f5c\u6210\u65b9\u6cd5\u306e\u30e1\u30e2\u3002<br \/>\nSQL\u81ea\u5206\u3067\u66f8\u304d\u305f\u3044\u30de\u30f3\u306b\u306f\u3001SQL\u304c\u898b\u3048\u306a\u3044\u5f62\u3067\u62bd\u8c61\u5316\u3055\u308c\u308b\u3068\u3001\u6700\u521d\u306f\u9055\u548c\u611f\u304c\u3042\u308a\u5927\u5909\u3067\u3042\u3063\u305f\u3002<\/p>\n<h3>\u30c7\u30fc\u30bf\u524d\u63d0<\/h3>\n<p>\u8a18\u4e8b\u3068\u30ab\u30c6\u30b4\u30ea(\u30ec\u30d3\u30e5\u30fc,\u65e5\u8a18\u306a\u3069)\u306e\u30c7\u30fc\u30bf\u304c\u3042\u308a\u3001\u591a\u5bfe\u591a\u306e\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u3092\u6301\u3064\u3068\u3059\u308b\u3002<br \/>\nthrough\u3067\u6307\u5b9a\u3059\u308b\u30d5\u30a3\u30fc\u30eb\u30c9\u306f\u5148\u306b\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002(article_categorys)<\/p>\n<pre class=\"lang:default decode:true \" >class Article &lt; ApplicationRecord\r\n  has_many :article_categorys\r\n  has_many :categorys, through: :article_categorys\r\nend\r\n\r\n\r\nclass Category &lt; ApplicationRecord\r\n  has_many :article_categorys\r\n  has_many :aricles, through: :article_categorys\r\nend\r\n<\/pre>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306a\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u308b\u3068\u3059\u308b\u3002<br \/>\nrails g model Article_Category article:references category:references<\/p>\n<pre class=\"lang:default decode:true \" >class ArticleCategory &lt; ApplicationRecord\r\n  belongs_to :article\r\n  belongs_to :category\r\nend<\/pre>\n<h3>\u4f5c\u6210\u65b9\u6cd5<\/h3>\n<p>\u3053\u306e\u6642\u306bseed.rd\u3067\u521d\u671f\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3059\u308b\u6642\u306e\u30e1\u30e2\u306b\u306a\u308b\u3002<br \/>\n\u95a2\u4fc2\u3092\u6301\u3064\u30c6\u30fc\u30d6\u30eb\u306eID\u3092\u5217\u6319\u3057\u3066\u305d\u308c\u3092\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u306b\u7a81\u3063\u8fbc\u3081\u3070\u826f\u3044\u3060\u3051\u3002<\/p>\n<pre class=\"lang:default decode:true \" ># \u30ab\u30c6\u30b4\u30ea\r\nCategory.create(name: '\u30b0\u30eb\u30e1')\r\nCategory.create(name: '\u89b3\u5149')\r\n\r\n# \u8a18\u4e8b\r\nArticle.create(title: '\u30bf\u30a4\u30c8\u30eb1', sub_title: '\u30b5\u30d6\u30bf\u30a4\u30c8\u30eb1', content: 'hoge')\r\n\r\n\r\n# \u8a18\u4e8b\u3001\u30ab\u30c6\u30b4\u30ea\u306eid\u3092\u5217\u6319\u3057\u3066\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u306b\u6295\u5165\r\nArticle.all.ids.sort.each do |article_id|\r\n  Category.all.ids.sort.each do |category_id|\r\n    ArticleCategory.create(article_id: article_id, category_id: category_id)\r\n  end\r\nend<\/pre>\n<p>\u5916\u90e8\u30ad\u30fc\u306e\u524a\u9664\u306b\u95a2\u3057\u3066\u3001\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u3057\u3066dependent: :destroy\u3092\u4ed8\u4e0e\u3059\u308b\u4e8b\u3067\u524a\u9664\u53ef\u80fd\u306b\u306a\u308b\u3002<br \/>\n\u4ed8\u4e0e\u3057\u3066\u3044\u306a\u3044\u72b6\u614b\u3067\u3001article_categorys\u306bid\u3092\u6301\u3064\u72b6\u614b\u3067Article\u3092destory\u3092\u3057\u305f\u6642\u306b\u306f\u5916\u90e8\u53c2\u7167\u30a8\u30e9\u30fc\u3068\u306a\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true \" >class Article\r\n  has_many :article_categorys, dependent: :destroy\r\n  has_many :categorys, through: :article_categorys\r\nend<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 Rails\u306eActiveRecord\u3067\u591a\u5bfe\u591a\u306e\u30c7\u30fc\u30bf\u3092\u5b9a\u7fa9\u3057\u305f\u6642\u306e\u30c7\u30fc\u30bf\u4f5c\u6210\u65b9\u6cd5\u306e\u30e1\u30e2\u3002 SQL\u81ea\u5206\u3067\u66f8\u304d\u305f\u3044\u30de\u30f3\u306b\u306f\u3001SQL\u304c\u898b\u3048\u306a\u3044\u5f62\u3067\u62bd\u8c61\u5316\u3055\u308c\u308b\u3068\u3001\u6700\u521d\u306f\u9055\u548c\u611f\u304c\u3042\u308a\u5927\u5909\u3067\u3042\u3063\u305f\u3002 \u30c7\u30fc &#8230; <\/p>\n","protected":false},"author":1,"featured_media":1278,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[32],"tags":[],"_links":{"self":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/1256"}],"collection":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1256"}],"version-history":[{"count":3,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/1256\/revisions"}],"predecessor-version":[{"id":1349,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/posts\/1256\/revisions\/1349"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=\/wp\/v2\/media\/1278"}],"wp:attachment":[{"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nobunobu1717.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}