{"id":7966,"date":"2019-12-07T08:06:26","date_gmt":"2019-12-06T23:06:26","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=7966"},"modified":"2023-05-27T22:54:21","modified_gmt":"2023-05-27T13:54:21","slug":"%e3%80%90ruby-on-rails%e3%80%91%e3%80%8cactive-storage%e3%80%8d%e3%81%ae%e5%9f%ba%e6%9c%ac","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=7966","title":{"rendered":"\u3010Ruby on Rails\u3011\u300cActive Storage\u300d\u306e\u57fa\u672c"},"content":{"rendered":"<h2>Active Storage\u3068\u306f\uff1f<\/h2>\n<p>5.2\u3067Rails\u3067\u5b9f\u88c5\u3055\u308c\u305fgem\u3067<span style=\"color: #ff0000;\"><strong>\u30af\u30e9\u30a6\u30c9\u30b9\u30c8\u30ec\u30fc\u30b8\u30b5\u30fc\u30d3\u30b9\uff08AWS\u306es3\u3001Google Colud Storage\u3001Microsoft Azure Storage\uff09\u3078\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3066ActiveRecord\u3067\u30e2\u30c7\u30eb\u306b\u7d10\u4ed8\u3051\u308b\u3068\u3044\u3046\u3053\u3068\u304c\u7c21\u5358\u306b\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/strong><\/span>\u3082\u3061\u308d\u3093\u3001\u958b\u767a\u74b0\u5883\u3067\u306f\u30ed\u30fc\u30ab\u30eb\u3001\u672c\u756a\u74b0\u5883\u306e\u307f\u5916\u90e8\u30b9\u30c8\u30ec\u30fc\u30b8\u30b5\u30fc\u30d3\u30b9\u3092\u4f7f\u3046\u3068\u8a00\u3063\u305f\u3088\u3046\u306a\u4f7f\u3044\u65b9\u304c\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>\u6271\u3048\u308b\u30d5\u30a1\u30a4\u30eb\u306e\u7a2e\u985e<\/h3>\n<p>\u3069\u3093\u306a\u7a2e\u985e\u306e\u30d5\u30a1\u30a4\u30eb\u3067\u3082\u6271\u3048\u307e\u3059\u3002<\/p>\n<h3>\u6a5f\u80fd\u8a73\u7d30<\/h3>\n<ul>\n<li>\u753b\u50cf\u306b\u3064\u3044\u3066\u306f\u30b5\u30a4\u30ba\u3084\u5f62\u5f0f\u5909\u63db\u3092\u884c\u3048\u308b\u3002<\/li>\n<li>\u30d3\u30c7\u30aa\u3084PDF\u306b\u3064\u3044\u3066\u306f\u30d7\u30ec\u30d3\u30e5\u30fc\u6a5f\u80fd\u3082\u63d0\u4f9b\u3057\u3066\u3044\u308b\u3002<\/li>\n<\/ul>\n<h2>Active Storage\u3067\u5b9f\u88c5\u3059\u308b\u3002<\/h2>\n<p>\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u65bd\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">rails active_storage:install<\/pre>\n<p>\u3059\u308b\u3068\u4e0b\u8a18\u306e\u3088\u3046\u306bdb\/migrate\/xxxx_create_active_storage_tables.active_storage.rb\u306b\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \"># This migration comes from active_storage (originally 20170806125915)\r\nclass CreateActiveStorageTables &lt; ActiveRecord::Migration[5.2]\r\n  def change\r\n    create_table :active_storage_blobs do |t|\r\n      t.string   :key,        null: false\r\n      t.string   :filename,   null: false\r\n      t.string   :content_type\r\n      t.text     :metadata\r\n      t.bigint   :byte_size,  null: false\r\n      t.string   :checksum,   null: false\r\n      t.datetime :created_at, null: false\r\n\r\n      t.index [ :key ], unique: true\r\n    end\r\n\r\n    create_table :active_storage_attachments do |t|\r\n      t.string     :name,     null: false\r\n      t.references :record,   null: false, polymorphic: true, index: false\r\n      t.references :blob,     null: false\r\n\r\n      t.datetime :created_at, null: false\r\n\r\n      t.index [ :record_type, :record_id, :name, :blob_id ], name: \"index_active_storage_attachments_uniqueness\", unique: true\r\n      t.foreign_key :active_storage_blobs, column: :blob_id\r\n    end\r\n  end\r\nend<\/pre>\n<p>Active Storage\u3067\u306f\u4e0b\u8a182\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u5229\u7528\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>active_storage_blobs<\/li>\n<li>active_storage_attachments<\/li>\n<\/ul>\n<h3>active_storage_blobs(\u300cActiveStorage::Blob\u300d\u3068\u3044\u3046\u30e2\u30c7\u30eb\u306b\u7d10\u3065\u304f\uff09<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>\u6dfb\u4ed8\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u306b\u5bfe\u5fdc\u3059\u308b\u30e2\u30c7\u30eb<\/strong><\/span>\u3067\u3059\u3002\u30d5\u30a1\u30a4\u30eb\u306e\u5b9f\u4f53\u3092DB\u5916\u3067\u7ba1\u7406\u3059\u308b\u3053\u3068\u3092\u524d\u63d0\u3068\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>active_storage_attachments(\u300cActiveStorage::Attachment\u300d\u3068\u3044\u3046\u30e2\u30c7\u30eb\u306b\u7d10\u3065\u304f\uff09<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>\u300cActiveStorage::Blob\u300d\u3068\u30a2\u30d7\u30ea\u5185\u306e\u69d8\u3005\u306a\u30e2\u30c7\u30eb\u3092\u95a2\u9023\u3065\u3051\u308b\u4e2d\u9593\u7684\u306a\u7acb\u3061\u4f4d\u7f6e\u306e\u30e2\u30c7\u30eb<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002\u4ed6\u306e\u30a2\u30d7\u30ea\u5185\u306e\u69d8\u3005\u306a\u30e2\u30c7\u30eb\u3068\u7d10\u4ed8\u3051\u308c\u308b\u3088\u3046\u306b\u95a2\u9023\u3059\u308b\u30e2\u30c7\u30eb\u306eFK\u30ab\u30e9\u30e0\u540d\uff08\u5916\u90e8\u30ad\u30fc\uff09\u3092FK\u5024\u3068\u3057\u3066\u4fdd\u6301\u3057\u307e\u3059\u3002\u306a\u304a\u3001ActiveStorage::Blob\u3068\u306f\u76f4\u63a5ID\u306e\u307f\u3067\u7d10\u4ed8\u3051\u307e\u3059\u3002<\/p>\n<p>\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u65bd\u3057\u3066\u4e0a\u8a18\u306e2\u30c6\u30fc\u30d6\u30eb\u3092DB\u306b\u53cd\u6620\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">rails db:migrate<\/pre>\n<h3>Active Storage\u306e\u8a2d\u5b9a\u3092\u3059\u308b\u3002<\/h3>\n<h4>\u4fdd\u5b58\u5834\u6240\u306e\u8a2d\u5b9a<\/h4>\n<h5>config\/environments\/development.rb<\/h5>\n<p>\u30d5\u30a1\u30a4\u30eb\u3092\u7ba1\u7406\u3059\u308b\u5834\u6240\u306e\u8a2d\u5b9a\u304c\u300clocal\u300d\u306b\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002local\u306f\u300cconfig\/storage.yml\u300d\u306b\u3066\u5834\u6240\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">  # Store uploaded files on the local file system (see config\/storage.yml for options)\r\n  config.active_storage.service = :local<\/pre>\n<h5>config\/storage.yml<\/h5>\n<p>\u4e0b\u8a18\u304clocal\u306e\u8a2d\u5b9a\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">local:\r\n  service: Disk\r\n  root: &lt;%= Rails.root.join(\"storage\") %&gt;<\/pre>\n<p>Rails\u30a2\u30d7\u30ea\u304c\u914d\u7f6e\u3055\u308c\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u914d\u4e0b\u306estorage\u3068\u3044\u3046\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u3059\u308b\u8a2d\u5b9a\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h4>\u30e2\u30c7\u30eb\u306e\u8a2d\u5b9a<\/h4>\n<p><strong>app\/models\/\u30e2\u30c7\u30eb\u540d.rb<\/strong><\/p>\n<pre class=\"lang:default decode:true\">class \u30e2\u30c7\u30eb\u540d &lt; ApplicationRecord\r\n  has_one_attached :image\u3000\u2605\r\n  \u2025\r\nend<\/pre>\n<p>\u300chas_one_attached\u300d\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u30671\u3064\u306e\u30bf\u30b9\u30af\u306b1\u3064\u306e\u753b\u50cf\u3092\u7d10\u3065\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p><strong>\u30d3\u30e5\u30fc\u30d5\u30a1\u30a4\u30eb\u5074\u306e\u8a2d\u5b9a<\/strong><\/p>\n<p><strong>\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5074\u306e\u8a2d\u5b9a<\/strong><\/p>\n<p>form_with\u3067\u3042\u308c\u3070\u4e0b\u8a18\u306e\u3088\u3046\u306b<span style=\"color: #ff0000;\"><strong>\u300cimage\u300d<\/strong><\/span>\u3068\u3044\u3046\u753b\u50cf\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u7528\u306e\u5165\u529b\u90e8\u54c1\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">= form_with model: @\u30e2\u30c7\u30eb\u540d, local: true do |f|\r\n  .form-group\r\n    = f.label :image\r\n    = f.file_field :image, class: 'form-control'<\/pre>\n<p><strong>\u8868\u793a\u5074\u306e\u8a2d\u5b9a<\/strong><\/p>\n<pre class=\"lang:default decode:true \">image_tag @\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u5909\u6570.image if @\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u5909\u6570.image.attached?<\/pre>\n<p>\u300cimage_tag\u30d3\u30e5\u30fc\u30d8\u30eb\u30d1\u30fc\u300d\u3092\u4f7f\u3063\u3066\u753b\u50cf\u3092\u8868\u793a\u3055\u305b\u307e\u3057\u3087\u3046\u3002\u306a\u304a\u3001\u753b\u50cf\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u306a\u3044\u5834\u5408\u3082\u5bfe\u5fdc\u3067\u304d\u308b\u3088\u3046\u306b\u300cif @\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u5909\u6570.image.attached?\u300d\u3068\u3044\u3046\u6587\u3092\u4ed8\u3051\u52a0\u3048\u3066\u78ba\u8a8d\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h4>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306e\u8a2d\u5b9a<\/h4>\n<pre class=\"lang:default decode:true\">params.require(:\u30e2\u30c7\u30eb\u540d).permit(:\u30ab\u30e9\u30e01,:\u30ab\u30e9\u30e02, :image\u2605)<\/pre>\n<p>Strong Parameter\u306b\u3088\u3063\u3066\u753b\u50cf\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3082\u8a31\u53ef\u3055\u308c\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Active Storage\u3068\u306f\uff1f 5.2\u3067Rails\u3067\u5b9f\u88c5\u3055\u308c\u305fgem\u3067\u30af\u30e9\u30a6\u30c9\u30b9\u30c8\u30ec\u30fc\u30b8\u30b5\u30fc\u30d3\u30b9\uff08AWS\u306es3\u3001Google Colud Storage\u3001Microsoft Azure Storage\uff09\u3078\u30d5\u30a1\u30a4\u30eb\u3092\u30a2 [&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\/7966"}],"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=7966"}],"version-history":[{"count":13,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7966\/revisions"}],"predecessor-version":[{"id":20529,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/7966\/revisions\/20529"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7966"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}