{"id":8127,"date":"2019-12-08T17:10:18","date_gmt":"2019-12-08T08:10:18","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=8127"},"modified":"2023-05-27T22:52:52","modified_gmt":"2023-05-27T13:52:52","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%80%8cseed%e3%83%87%e3%83%bc%e3%82%bf%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=8127","title":{"rendered":"\u3010Ruby on Rails\u3011\u300cseed\u30c7\u30fc\u30bf\u300d\u3001\u300cfaker\u300d"},"content":{"rendered":"<h2>seed\u30c7\u30fc\u30bf\u3068\u306f<\/h2>\n<p><span style=\"color: #ff0000;\"><strong>\u521d\u671f\u30c7\u30fc\u30bf\u3084\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf<\/strong><\/span>\u306e\u3053\u3068\u3067\u52d5\u4f5c\u78ba\u8a8d\u7528\u30c7\u30fc\u30bf\u3092\u767b\u9332\u3059\u308b\u76ee\u7684\u3067\u884c\u3044\u307e\u3059\u3002\u6295\u5165\u3057\u305f\u3044\u30c7\u30fc\u30bf\u306e\u4ef6\u6570\u304c\u5897\u3048\u305f\u5834\u5408\u306b\u3082\u3053\u308c\u3055\u3048\u3042\u308c\u3070\u8fc5\u901f\u306b\u6295\u5165\u3067\u304d\u308b\u74b0\u5883\u3092\u69cb\u7bc9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>seed\u30c7\u30fc\u30bf\u306e\u6295\u5165\u65b9\u6cd5<\/h2>\n<p>Rails\u306b\u306f\u6295\u5165\u7528\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3002\u4e00\u5ea6\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u8a18\u8ff0\u3057\u3066\u3057\u307e\u3048\u3070db\/seeds.rb\u3084rails db:seed\u30b3\u30de\u30f3\u30c9\u306b\u3088\u308a\u6295\u5165\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4e0b\u8a18\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<div>\n<pre class=\"lang:default decode:true\">\u30e2\u30c7\u30eb\u540d.create(id: XX ,\u30ab\u30e9\u30e0\u540d2: 'XX',\u30ab\u30e9\u30e0\u540d3: 'XX')\r\n\u30e2\u30c7\u30eb\u540d.create(id: XX ,\u30ab\u30e9\u30e0\u540d2: 'XX',\u30ab\u30e9\u30e0\u540d3: 'XX')\r\n\u30e2\u30c7\u30eb\u540d.create(id: XX ,\u30ab\u30e9\u30e0\u540d2: 'XX',\u30ab\u30e9\u30e0\u540d3: 'XX')<\/pre>\n<p>\u4f8b\uff1aUser.create(id: 1,name: 'tanaka',age: 23)<\/p>\n<\/div>\n<h3>\u5927\u91cf\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3059\u308b\u5834\u5408<\/h3>\n<p>\u5927\u91cf\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3059\u308b\u5834\u5408\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u3066\u8a18\u8ff0\u306e\u52b9\u7387\u5316\u3092\u6e2c\u3063\u305f\u308a\u3082\u3057\u307e\u3059\u3002\uff08\u4f8b\u3067\u306f100\u4ef6\u306e\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3059\u308b\u5834\u5408\uff09<\/p>\n<pre class=\"lang:default decode:true\">  (1..100).each do |i|\r\n    Board.create(name: \"\u540d\u524d#{i}\",age: \"\u5e74\u9f62#{i}\")\r\n  end<\/pre>\n<h3>\u74b0\u5883\u3054\u3068\u306b\u6295\u5165\u3059\u308b\u30c7\u30fc\u30bf\u3092\u5909\u3048\u305f\u3044\u5834\u5408<\/h3>\n<p>\u4f8b\u3048\u3070\u958b\u767a\u74b0\u5883\u306e\u307f\u3068\u3044\u3046\u5f62\u3067\u74b0\u5883\u3054\u3068\u306b\u6295\u5165\u3059\u308b\u30c7\u30fc\u30bf\u3092\u30d5\u30a3\u30eb\u30bf\u3057\u305f\u3044\u5834\u5408\u306f\u4e0b\u8a18\u306e\u8a18\u8ff0\u3092\u3057\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u8aa4\u3063\u305f\u30c7\u30fc\u30bf\u306e\u6295\u5165\u3092\u9632\u3050\u3053\u3068\u304c\u3067\u304d\u305f\u308a\u3057\u307e\u3059\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">if Rails.env == 'development'\r\nend<\/pre>\n<p>\u73fe\u5b9f\u7684\u306b\u306f\u3001\u300cdb\/seeds\u300d\u306e\u4e0b\u306b\u300cdevelopment\u300d\u3001\u300ctest\u300d\u3001\u300cproduction\u300d\u306a\u3069\u3068\u30d5\u30a9\u30eb\u30c0\u3092\u5207\u3063\u3066\u5206\u3051\u308b\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>seed\u30c7\u30fc\u30bf\u306e\u6295\u5165<\/h2>\n<p>\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">rake db:seed<\/pre>\n<p>docker\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u4e0b\u8a18\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">docker-compose exec web bundle exec rake db:seed<\/pre>\n<h2>\u305d\u308c\u4ee5\u5916\u306b\u3082\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3059\u308b\u65b9\u6cd5<\/h2>\n<p>Rails\u306b\u306fseed\u4ee5\u5916\u306b\u3082SQL\u3084\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3001Factory Bot\u3084\u4ee3\u66ff\u3059\u308bGem\u7b49\u3001\u69d8\u3005\u306a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u6e96\u5099\u3059\u308b\u305f\u3081\u306e\u4ed5\u7d44\u307f\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u72b6\u6cc1\u306b\u5408\u308f\u305b\u3066\u4f7f\u3044\u3084\u3059\u3044\u3082\u306e\u3092\u5229\u7528\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u6ce8\u610f\u70b9<\/h2>\n<p>\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u81ea\u4f53\u306f\u4e00\u5ea6\u6700\u521d\u306b\u7528\u610f\u3057\u3066\u3057\u307e\u3063\u305f\u3089\u5fc5\u8981\u304c\u306a\u304f\u306a\u308b\u305f\u3081\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u4e2d\u3067\u306f\u653e\u7f6e\u3055\u308c\u3084\u3059\u3044\u5206\u91ce\u306b\u306a\u308a\u307e\u3059\u3002DB\u69cb\u9020\u304c\u5909\u308f\u308b\u3053\u3068\u3082\u3042\u308c\u3070\u3001\u30c7\u30fc\u30bf\u306e\u6301\u3061\u65b9\u304c\u5909\u308f\u308b\u53ef\u80fd\u6027\u3082\u3042\u308a\u5f8c\u306e\u30e1\u30f3\u30d0\u30fc\u306e\u305f\u3081\u306b\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u3092\u3057\u3063\u304b\u308a\u3057\u3066\u3044\u304f\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>faker<\/h2>\n<p>\u30c0\u30df\u30fc\u30c7\u30fc\u30bf\u3092\u4f5c\u308c\u308bGem\u3067\u3059\u3002<\/p>\n<h3>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<pre class=\"lang:default decode:true\">group :development do\r\n  gem 'faker'\r\nend<\/pre>\n<h3>\u69cb\u6587<\/h3>\n<pre class=\"lang:default decode:true\">Faker::[\u30b8\u30e3\u30f3\u30eb].[\u30bf\u30a4\u30c8\u30eb\u306a\u3069]<\/pre>\n<h3>\u3069\u3093\u306a\u30b8\u30e3\u30f3\u30eb\u304c\u3042\u308b\u304b\u8abf\u3079\u308b\u306b\u306f\uff1f<\/h3>\n<p>github\u306e\u4ee5\u4e0b\u306e\u30da\u30fc\u30b8\u306b\u884c\u3051\u3070\u4e00\u89a7\u3067\u4f7f\u3048\u308b\u30b8\u30e3\u30f3\u30eb\u306e\u30af\u30e9\u30b9\u304c\u8a18\u8f09\u3057\u3066\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">https:\/\/github.com\/faker-ruby\/faker<\/pre>\n<h3>\u4f8b<\/h3>\n<h4>Movie<\/h4>\n<pre class=\"lang:default decode:true \">irb(main):003:0&gt; Faker::Movie.title\r\n=&gt; \"Star Wars: Episode II \u2013 Attack of the Clones\"\r\nirb(main):004:0&gt; Faker::Movie.title\r\n=&gt; \"The Intouchables\"\r\nirb(main):005:0&gt; Faker::Movie.title\r\n=&gt; \"Hotel Rwanda\"<\/pre>\n<h3>Pokemon<\/h3>\n<pre class=\"lang:default decode:true \">irb(main):017:0&gt; Faker::Games::Pokemon.name\r\n=&gt; \"Graveler\"\r\nirb(main):018:0&gt; Faker::Games::Pokemon.name\r\n=&gt; \"Nidorina\"<\/pre>\n<h3>Book<\/h3>\n<pre class=\"lang:default decode:true\">irb(main):027:0&gt; Faker::Book.title\r\n=&gt; \"Mother Night\"\r\nirb(main):028:0&gt; Faker::Book.title\r\n=&gt; \"All the King's Men\"<\/pre>\n<h3>\u65e5\u672c\u8a9e\u5316<\/h3>\n<pre class=\"lang:default decode:true \">Faker::Config.locale = :ja\r\nirb(main):044:0&gt; Faker::Address.city\r\n=&gt; \"\u65b0\u4e95\u90e1\"\r\nirb(main):045:0&gt; Faker::Address.city\r\n=&gt; \"\u9678\u753a\"<\/pre>\n<h3>\u5b9f\u52d9\u3092\u610f\u8b58\u3059\u308b\u306a\u3089<\/h3>\n<p>\u5b9f\u52d9\u3067\u5b9f\u969b\u306b\u4f7f\u3046\u306a\u3089\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30c7\u30fc\u30bf\u304c\u4f7f\u3048\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<ul>\n<li>\u4f4f\u6240<\/li>\n<li>\u540d\u524d<\/li>\n<li>\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"seed\u30c7\u30fc\u30bf\u3068\u306f \u521d\u671f\u30c7\u30fc\u30bf\u3084\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u3053\u3068\u3067\u52d5\u4f5c\u78ba\u8a8d\u7528\u30c7\u30fc\u30bf\u3092\u767b\u9332\u3059\u308b\u76ee\u7684\u3067\u884c\u3044\u307e\u3059\u3002\u6295\u5165\u3057\u305f\u3044\u30c7\u30fc\u30bf\u306e\u4ef6\u6570\u304c\u5897\u3048\u305f\u5834\u5408\u306b\u3082\u3053\u308c\u3055\u3048\u3042\u308c\u3070\u8fc5\u901f\u306b\u6295\u5165\u3067\u304d\u308b\u74b0\u5883\u3092\u69cb\u7bc9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 seed\u30c7\u30fc\u30bf\u306e\u6295\u5165\u65b9\u6cd5  [&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\/8127"}],"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=8127"}],"version-history":[{"count":12,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/8127\/revisions"}],"predecessor-version":[{"id":20525,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/8127\/revisions\/20525"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8127"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}