{"id":12427,"date":"2020-12-19T14:20:52","date_gmt":"2020-12-19T05:20:52","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=12427"},"modified":"2023-05-27T22:42:56","modified_gmt":"2023-05-27T13:42:56","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%80%8c%e5%a4%96%e9%83%a8%e3%82%ad%e3%83%bc%e3%80%8d%e3%81%ae%e8%a8%ad%e5%ae%9a%e3%81%ae%e3%81%95%e3%82%8c%e6%96%b9","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=12427","title":{"rendered":"\u3010Ruby on Rails\u3011\u300c\u5916\u90e8\u30ad\u30fc\u300d\u306e\u8a2d\u5b9a\u306e\u3055\u308c\u65b9"},"content":{"rendered":"<p>\u5916\u90e8\u30ad\u30fc\u306f\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3067\u8a2d\u5b9a\u3059\u308b\u5834\u5408\u3068\u3001\u30e2\u30c7\u30eb\u5074\u3067Rails\u306e\u6a5f\u80fd\u3068\u3057\u3066\u8a2d\u5b9a\u3059\u308b\u5834\u5408\u306e2\u7a2e\u985e\u3042\u308a\u307e\u3059\u3002\u5b9f\u52d9\u3067\u306f<span style=\"color: #ff0000;\"><strong>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u306f\u8a2d\u5b9a\u3057\u306a\u3044\u50be\u5411\u306b\u3042\u308b\u3088\u3046\u306b\u601d\u3048\u307e\u3059\u3002\uff08\u30c7\u30fc\u30bf\u306e\u7528\u610f\u304c\u9762\u5012\u306b\u306a\u308a\u307e\u3059\u304b\u3089\u306d\u3002\uff09<\/strong><\/span><\/p>\n<h2>\u300c\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u300d\u3067\u5916\u90e8\u30ad\u30fc\u3092\u8a2d\u5b9a\u3057\u305f\u3044\u5834\u5408<\/h2>\n<p><span style=\"color: #ff0000;\"><strong>\u300cuser:post\uff1d1\uff1a\u591a\u300d<\/strong><\/span>\u306e\u5834\u5408<\/p>\n<h3><strong>reference\u578b\u3092\u4f7f\u3046\u5834\u5408<\/strong><\/h3>\n<pre class=\"lang:default decode:true\">class CreatePosts &lt; ActiveRecord::Migration[6.0]\r\n  def change\r\n    create_table :posts do |t|\r\n      t.string :name\r\n      t.references :user,foreign_key:true\r\n\r\n      t.timestamps\r\n    end\r\n  end\r\nend\r\n<\/pre>\n<h4>reference\u578b\u306e\u4ed5\u69d8<\/h4>\n<ul>\n<li>\u300c\u30e2\u30c7\u30eb\u540d_id\u300d\u3068\u3044\u3046\u30ab\u30e9\u30e0\u540d\u3092\u81ea\u52d5\u3067\u751f\u6210\u3059\u308b\u3002<\/li>\n<li>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u81ea\u52d5\u3067\u4ed8\u4e0e\u3057\u3066\u304f\u308c\u308b\u3002<\/li>\n<li>\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u306f\u4ed8\u4e0e\u3057\u306a\u3044\u3002\uff08\u3057\u305f\u3044\u5834\u5408\u306f\u300cforeign_key:true\u300d\u3068\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\uff09<\/li>\n<\/ul>\n<h3><strong>reference\u578b\u3092\u4f7f\u308f\u306a\u3044\u5834\u5408<\/strong><\/h3>\n<pre class=\"lang:default decode:true \">class CreatePosts &lt; ActiveRecord::Migration[6.0]\r\n  def change\r\n    create_table :posts do |t|\r\n      t.string :name\r\n      t.integer :user_id\r\n\r\n      t.timestamps\r\n    end\r\n    add_foreign_key :posts, :users # add_foreign_key :\u5bfe\u8c61\u306e\u30c6\u30fc\u30d6\u30eb\u540d, :\u6307\u5b9a\u5148\u306e\u30c6\u30fc\u30d6\u30eb \r\n  end\r\nend<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>add_foreign_key<\/strong><\/span>\u3092\u4f7f\u3048\u3070\u3001post_id\u306b\u306f\u81ea\u52d5\u3067\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u4ed8\u4e0e\u3055\u308c\u308b\u306e\u3067<span style=\"color: #ff0000;\"><strong>\u300cadd_index\u300d<\/strong><\/span>\u3084<span style=\"color: #ff0000;\"><strong>\u300cindex:true\u300d<\/strong><\/span>\u304c\u4e0d\u8981\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u300c\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u300d\u3067\u306e\u5916\u90e8\u30ad\u30fc\u306e\u6c7a\u5b9a\u306e\u3055\u308c\u65b9\uff08\u30e2\u30c7\u30eb\u5074\uff09<\/h2>\n<h3>\u901a\u5e38<\/h3>\n<p>\uff11\uff1a\u591a\u306e\u591a\u306e\u65b9\u306e\u30c6\u30fc\u30d6\u30eb\u3067\u300c\u53c2\u7167\u5148\u306e\u30e2\u30c7\u30eb\u540d\u300d + \u300c_id\u300d\u3068\u3044\u3046\u540d\u524d\u306e\u30ab\u30e9\u30e0\u3092\u4ed8\u3051\u305f\u3089\u81ea\u52d5\u7684\u306b\u5916\u90e8\u30ad\u30fc\u3068\u6c7a\u5b9a\u3055\u308c\u308b\u3002<\/p>\n<h3>\u7570\u306a\u308b\u30ab\u30e9\u30e0\u3092\u5916\u90e8\u30ad\u30fc\u306b\u3057\u305f\u3044\u5834\u5408<\/h3>\n<pre class=\"lang:default decode:true \">class \u89aa\u30e2\u30c7\u30eb\u540d &lt; ActiveRecord::Base\r\n    \u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3 :\u5b50\u30e2\u30c7\u30eb\u540d, foreign_key: '\u89aa\u30e2\u30c7\u30eb\u304c\u5b50\u30e2\u30c7\u30eb\u306e\u4e2d\u3067\u53c2\u7167\u3059\u308b\u5916\u90e8\u30ad\u30fc\u540d'\r\nend<\/pre>\n<h3>\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3<\/h3>\n<p>has_many\u3084\u3001has_one\u3001belongs_to\u306e\u4e8b\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5916\u90e8\u30ad\u30fc\u306f\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3067\u8a2d\u5b9a\u3059\u308b\u5834\u5408\u3068\u3001\u30e2\u30c7\u30eb\u5074\u3067Rails\u306e\u6a5f\u80fd\u3068\u3057\u3066\u8a2d\u5b9a\u3059\u308b\u5834\u5408\u306e2\u7a2e\u985e\u3042\u308a\u307e\u3059\u3002\u5b9f\u52d9\u3067\u306f\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u306f\u8a2d\u5b9a\u3057\u306a\u3044\u50be\u5411\u306b\u3042\u308b\u3088\u3046\u306b\u601d\u3048\u307e\u3059\u3002\uff08\u30c7\u30fc\u30bf\u306e\u7528\u610f\u304c\u9762\u5012\u306b\u306a\u308a\u307e\u3059\u304b\u3089\u306d [&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\/12427"}],"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=12427"}],"version-history":[{"count":8,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12427\/revisions"}],"predecessor-version":[{"id":20495,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/12427\/revisions\/20495"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12427"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}