[RUBY] [Rails] Yeah! Active Strage! Competent Gem! ~ You can save images from two tables ~

It was a mystery all the time

Is it possible to save from two tables when saving an image with Active Strage? I was always wondering. We are currently developing an original app, but since it is a review site, ** ① User profile photo ** ** ② Photos to be reviewed ** I wanted to save these two, but I wasn't sure how to save from multiple tables, so the user saved it as a text type and used Active Strage only for review. Today, I made a mistake that I had to roll back the migration, so I decided to try it, and did the following work.

・ Delete the column that was supposed to save the image saved in the user table ・ Check if photos can be saved at the time of user registration & review target registration

First, edit the migration.

% rails db:rollback  

This command was very, very scary to run at first (some beginners) After that, check the status to see what the situation is like.

% rails db:migrate:status  

database: party_freak_development

 Status   Migration ID    Migration Name
--------------------------------------------------
up 20210107124355 Devise create users ⬅️ Change goal is here
   up     20210108071047  Create parties 
   up     20210108094951  Create active storage tablesactive storage
up 20210108122152 Rename iintroduction column to parties ⬅️ Renamed history
up 20210108123328 Rename date id column to parties ⬅️ Renamed history
down 20210108130921 Change column to allow null ⬅️ null Constraint change history

Below are the migration files for the table column names and null constraints that we changed yesterday. If you think about it carefully and rename it to increase the number of migration files, it is better to roll back and modify the migration file directly so that the number of unnecessary files does not increase. I noticed that, I repeated rollback and right-clicked → deleted the following three migration files from the code editor. Refreshing.

This command is useful when repeating.

 % rails db:rollback STEP=5 
                ⬆️=After that, set down to 0(Count as 1 from the next up)Rollback that number of times
It was 5 in my case this time

If you look closely, you'll be impatient for a moment

== 20210108094951 CreateActiveStorageTables: reverting ========================
-- drop_table(:active_storage_attachments, {})
   -> 0.0470s
-- drop_table(:active_storage_blobs, {})
   -> 0.0363s
== 20210108094951 CreateActiveStorageTables: reverted (0.0847s) ===============

** Active Strage is a drop UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU! ** ** ** I did it again ah ah ah ah ah ah ah! ** ** I can't help erasing it, I'll be back again anyway I can only see before (confirmation criminal) You can meet again at db: migrate, right? Believe that and move on.

Write a description that enables Active Strage to be used in the user model.

app/models/user.rb


class User < ApplicationRecord
  
  has_one_attached :image ⬅️ Describe this

~ The following is omitted ~
  
end

** ① User model ** ** ② Review posting model ** I wrote has_one_attached: image in a total of two models.

Then delete the description for saving the image from the migration file and do db: migrate.

result

5d39289236b741ec2dcc8f2c4ed856eb.png

There are two displays in the column called record_type: ** User ** and ** Party **.

Great success! (I met you after all Active Strage

What I was interested in

I'm thinking that users can post multiple images (or videos), but will I create another table and save it there using has_many_attached? It is said that if there are multiple cases, it will change from has_one to has_many, but looking at this column seems to be possible, but I will write an article here again for the results I actually tried. I will.

That's how Active Strage saves images from two tables.

Recommended Posts

[Rails] Yeah! Active Strage! Competent Gem! ~ You can save images from two tables ~
[Ruby on Rails] Delete s3 images with Active Strage
Save data from excel file using Rails gem roo
Add, save, delete, add columns, save images from rails DB creation
[Rails] Save information from one form to multiple tables