{"id":8004,"date":"2019-12-07T16:02:03","date_gmt":"2019-12-07T07:02:03","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=8004"},"modified":"2019-12-07T17:01:28","modified_gmt":"2019-12-07T08:01:28","slug":"%e3%80%90ruby-on-rails%e3%80%91csv%e3%82%a4%e3%83%b3%e3%83%9d%e3%83%bc%e3%83%88%e3%82%84%e3%82%a8%e3%82%af%e3%82%b9%e3%83%9d%e3%83%bc%e3%83%88%e6%a9%9f%e8%83%bd%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=8004","title":{"rendered":"\u3010Ruby on Rails\u3011CSV\u30a4\u30f3\u30dd\u30fc\u30c8\u3084\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u6a5f\u80fd\u306b\u3064\u3044\u3066"},"content":{"rendered":"<p>Rails\u3067CSV\u306e\u30a4\u30f3\u30dd\u30fc\u30c8\u3001\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3092\u884c\u3046\u5834\u5408\u306fRuby\u306ecsv\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h2>csv\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u4e8b\u524d\u6e96\u5099<\/h2>\n<h3>config\/application.rb<\/h3>\n<p>\u4e0b\u8a18\u306erequire\u6587\u3092\u8ffd\u52a0\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:default decode:true \">require 'csv'<\/pre>\n<p>Rails\u3092\u518d\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n<h2>\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u6a5f\u80fd\u306e\u5b9f\u88c5<\/h2>\n<h3>\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u5074<\/h3>\n<p>\u30e2\u30c7\u30eb\u30af\u30e9\u30b9(app\/models\/\u30e2\u30c7\u30eb\u540d.rb\uff09\u306e\u4e2d\u3067\u30e2\u30c7\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066CSV\u6587\u5b57\u5217\u3092\u8fd4\u3059\u3088\u3046\u306b\u3057\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>all\u30e1\u30bd\u30c3\u30c9<\/strong><\/span>\u3068\u304b\u3067\u5168\u30ec\u30b3\u30fc\u30c9\u3092\u53d6\u5f97\u3057\u305f\u308a\u3059\u308b\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h4>CSV.generate\u30e1\u30bd\u30c3\u30c9<\/h4>\n<p>CSV\u6587\u5b57\u5217\u3092\u751f\u6210\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<h3>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u5074<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306a\u8a18\u8ff0\u3092\u3057\u3066\u8a72\u5f53URL\u306bcsv\u5f62\u5f0f\u3067\u6307\u5b9a\u3055\u308c\u305f\u5834\u5408\u306e\u307fcsv\u30d5\u30a1\u30a4\u30eb\u3092\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b\u3088\u3046\u306a\u8a2d\u5b9a\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:default decode:true\">    respond_to do |format|\r\n      format.html\r\n      format.csv { send_data @\u30e2\u30c7\u30eb\u540d.csv\u751f\u6210\u30e1\u30bd\u30c3\u30c9\u540d, filename: \"csv\u30d5\u30a1\u30a4\u30eb\u540d-#{Time.zone.now.strftime('%Y%m%d%S')}.csv\" }\r\n    end<\/pre>\n<p>\u306a\u304a\u3001HTML\u3067\u30a2\u30af\u30bb\u30b9\u3055\u308c\u305f\u5834\u5408\u306fformat.html\u304c\u52d5\u4f5c\u3057\u901a\u5e38\u306eHTML\u30da\u30fc\u30b8\u304c\u8fd4\u308a\u307e\u3059\u3002<\/p>\n<h3>\u30d3\u30e5\u30fc\u5074<\/h3>\n<pre class=\"lang:default decode:true \">= link_to '\u51fa\u529b', \u300cformat: :csv\u300d\u3092\u6307\u5b9a\u3057\u305fURL<\/pre>\n<p>\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u305fURL\u753b\u9762\u3092<span style=\"color: #ff0000;\"><strong>\u300cformat:csv\u300d<\/strong><\/span>\u306b\u3088\u308a\u547c\u3073\u51fa\u3057\u307e\u3057\u3087\u3046\u3002\u3059\u308b\u3068CSV\u51fa\u529b\u304c\u81ea\u52d5\u3067\u884c\u308f\u308c\u307e\u3059\u3002<\/p>\n<h2>\u30a4\u30f3\u30dd\u30fc\u30c8\u6a5f\u80fd\u306e\u5b9f\u88c5<\/h2>\n<h3>\u30e2\u30c7\u30eb\u30af\u30e9\u30b9<\/h3>\n<p>\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306b\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">  def self.import(file)\r\n    CSV.foreach(file.path, headers: true) do |row|\r\n      \u30e2\u30c7\u30eb\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u540d = new\r\n      \u30e2\u30c7\u30eb\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u540d.attributes = row.to_hash.slice(*csv_attributes)\r\n      \u30e2\u30c7\u30eb\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u540d.save!\r\n    end\r\n  end<\/pre>\n<h4>file<\/h4>\n<p>\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u305fCSV\u30d5\u30a1\u30a4\u30eb\u5185\u5bb9\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002\u30d5\u30a1\u30a4\u30eb\u5185\u5bb9\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002<\/p>\n<h4>CSV.foreach<\/h4>\n<p>CSV\u30d5\u30a1\u30a4\u30eb\u304b\u30891\u884c\u305a\u3064\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002\u300cheaders: true\u300d\u306b\u3088\u308a1\u884c\u76ee\u3092\u30d8\u30c3\u30c0\u884c\u3068\u3057\u3066\u7121\u8996\u3067\u304d\u307e\u3059\u3002<\/p>\n<h4>\u30e2\u30c7\u30eb\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u540d.attributes<\/h4>\n<p>\u30e2\u30c7\u30eb\u30a4\u30f3\u30b9\u30bf\u30f3\u306e\u5404\u5c5e\u6027\u306bcsv\u4e00\u884c\u306e\u60c5\u5831\u3092\u52a0\u5de5\u3057\u3066\u5165\u308c\u8fbc\u307f\u307e\u3059\u3002<\/p>\n<h4>to_hash\u30e1\u30bd\u30c3\u30c9\u3068slice\u30e1\u30bd\u30c3\u30c9<\/h4>\n<p>to_hash\u30e1\u30bd\u30c3\u30c9\u3067CSV\u306e\u30c7\u30fc\u30bf\u3092\u30cf\u30c3\u30b7\u30e5\u306e\u5f62\uff08{\u30ad\u30fc =&gt;\"\u5024\"}\uff09\u306b\u6574\u5f62\u3057\u307e\u3059\u3002slice\u30e1\u30bd\u30c3\u30c9\u3067\u672c\u6765\u30a4\u30f3\u30d7\u30c3\u30c8\u3059\u308b\u3064\u3082\u308a\u304c\u306a\u304b\u3063\u305f\u5c5e\u6027\u306b\u3064\u3044\u3066\u306f\u6392\u9664\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h4>\u30e2\u30c7\u30eb\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u540d.save!<\/h4>\n<p>\u30e2\u30c7\u30eb\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092DB\u306b\u767b\u9332\u3057\u307e\u3059\u3002<\/p>\n<h3>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u5074<\/h3>\n<p>\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u5074\u3067\u4f5c\u3063\u305fimport\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u307e\u3057\u3087\u3046\u3002\u305d\u306e\u969b\u306f<span style=\"color: #ff0000;\"><strong>\u300cparams[:file]\u300d<\/strong><\/span>\u7b49\u3067\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066CSV\u30d5\u30a1\u30a4\u30eb\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6e21\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Rails\u3067CSV\u306e\u30a4\u30f3\u30dd\u30fc\u30c8\u3001\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3092\u884c\u3046\u5834\u5408\u306fRuby\u306ecsv\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 csv\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u4e8b\u524d\u6e96\u5099 config\/application.rb \u4e0b\u8a18\u306erequire\u6587\u3092\u8ffd\u52a0\u3057\u307e\u3057\u3087\u3046\u3002  [&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\/8004"}],"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=8004"}],"version-history":[{"count":8,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/8004\/revisions"}],"predecessor-version":[{"id":8012,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/8004\/revisions\/8012"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8004"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}