LoginSignup
0
0

More than 3 years have passed since last update.

CarrierWaveによって保存されたデータの中身。

Posted at

調査した背景

CarrierWaveによって画像データをアップロードしたのですが、そのデータを更新したいとき、どのようにデータが入っているか調べました。
(この記事を見られている方ももしかすると、参照された方もいるかもしれません)

データベースでは以下のとおり、ファイル名のみが表示されます(image列)
image.png

DBのUIは「sequelpro」を使っています。列値の詳細を参照しても、ファイル名のみです。
image.png

環境

項目 内容
OS.Catalina v10.15.4
Ruby v2.5.1
Ruby On Rails v5.2.4.3
MySQL v5.6

中身を参照する

以下の通り、コマンドを実行し、中身を参照しました。

「Attachment」というテーブルにアクセスしています。

[6] pry(main)> >> image_data = Attachment.find(7)
image_data = Attachment.find(7)
  Attachment Load (0.5ms)  SELECT  `attachments`.* FROM `attachments` WHERE `attachments`.`id` = 7 LIMIT 1
=> #<Attachment:0x00007f86e6d7b760
 id: 7,
 knowledge_id: 17,
 sub_id: "1",
 name: "test.png",
 width_size: "1200",
 height_size: "799",
 file_type: "png",
 file_size: "72297",
 image: "test.png",
 thumb_image_url:
  "/uploads/tmp/1593690046-968881373703887-0012-2842/thumb_test.png",
 created_at: Thu, 02 Jul 2020 11:40:46 UTC +00:00,
 updated_at: Thu, 02 Jul 2020 11:40:46 UTC +00:00,
 image_url: "/uploads/tmp/1593690046-968881373703887-0012-2842/test.png">

ここからが、イメージ情報になります。

[7] pry(main)> >> image_data.image
image_data.image
=> #<ImageUploader:0x00007f86e6c26a68
 @cache_id=nil,
 @file=
  #<CarrierWave::SanitizedFile:0x00007f86e6c25e10
   @content=nil,
   @content_type=nil,
   @file=
    "/Users/ichikawadaisuke/projects/krown/public/uploads/attachment/image/7/test.png",
   @original_filename=nil>,
 @filename=nil,
 @format=nil,
 @identifier="test.png",
 @model=
  #<Attachment:0x00007f86e6d7b760
   id: 7,
   knowledge_id: 17,
   sub_id: "1",
   name: "test.png",
   width_size: "1200",
   height_size: "799",
   file_type: "png",
   file_size: "72297",
   image: "test.png",
   thumb_image_url:
    "/uploads/tmp/1593690046-968881373703887-0012-2842/thumb_test.png",
   created_at: Thu, 02 Jul 2020 11:40:46 UTC +00:00,
   updated_at: Thu, 02 Jul 2020 11:40:46 UTC +00:00,
   image_url: "/uploads/tmp/1593690046-968881373703887-0012-2842/test.png">,
 @mounted_as=:image,
 @staged=false,
 @storage=
  #<CarrierWave::Storage::File:0x00007f86e6c262c0
   @cache_called=nil,
   @uploader=#<ImageUploader:0x00007f86e6c26a68 ...>>,
 @versions=
  {:thumb=>
    #<ImageUploader::Uploader70108727518060:0x00007f86e6c25c80
     @cache_id=nil,
     @file=
      #<CarrierWave::SanitizedFile:0x00007f86e6c25460
       @content=nil,
       @content_type=nil,
       @file=
        "/Users/ichikawadaisuke/projects/krown/public/uploads/attachment/image/7/thumb_test.png",
       @original_filename=nil>,
     @filename=nil,
     @format=nil,
     @identifier="test.png",
     @model=
      #<Attachment:0x00007f86e6d7b760
       id: 7,
       knowledge_id: 17,
       sub_id: "1",
       name: "test.png",
       width_size: "1200",
       height_size: "799",
       file_type: "png",
       file_size: "72297",
       image: "test.png",
       thumb_image_url:
        "/uploads/tmp/1593690046-968881373703887-0012-2842/thumb_test.png",
       created_at: Thu, 02 Jul 2020 11:40:46 UTC +00:00,
       updated_at: Thu, 02 Jul 2020 11:40:46 UTC +00:00,
       image_url:
        "/uploads/tmp/1593690046-968881373703887-0012-2842/test.png">,
     @mounted_as=:image,
     @parent_version=#<ImageUploader:0x00007f86e6c26a68 ...>,
     @staged=false,
     @storage=
      #<CarrierWave::Storage::File:0x00007f86e6c25a00
       @cache_called=nil,
       @uploader=
        #<ImageUploader::Uploader70108727518060:0x00007f86e6c25c80 ...>>,
     @versions={}>}>
[8] pry(main)> 

さらにワンライナーで、簡単にオブジェクトの情報を取得出来ます。

[9] pry(main)> >> image_data.image.file
image_data.image.file
=> #<CarrierWave::SanitizedFile:0x00007f86e6c25e10
 @content=nil,
 @content_type=nil,
 @file=
  "/Users/ichikawadaisuke/projects/krown/public/uploads/attachment/image/7/test.png",
 @original_filename=nil>
[10] pry(main)> 


今回は以上です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0