[Ruby] The contents of the data stored by CarrierWave.

1 minute read

Background investigated

I uploaded image data using CarrierWave, and when I wanted to update that data, I checked how the data was stored. (Maybe some people have seen this article, others may have referred to it)

In the database, only the file name is displayed as follows (image column) ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/647299/a0350dd9-90f2-4b74-421c-f4cf12bdd801.png) DB UI uses "sequel pro". Even if you refer to the details of the column value, only the file name. ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/647299/0222101e-9ae5-6188-c189-8dd93882fa5b.png) ### Environment | Item | Content | |:-----------------|:------------------:| | OS.Catalina | v10.15.4 | | Ruby | v2.5.1 | | Ruby On Rails | v5.2.4.3 | | MySQL | v5.6 | ### Reference contents I ran the command as follows and referenced the contents. I am accessing a table called "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"> ``` Image information starts from here. ``` [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)> ``` Furthermore, you can easily get object information with one liner. ``` [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)> ``` This time is over.