rails active storage get all attachment names

To get all attachment names using Active Storage in Ruby on Rails, you can follow these steps:

Step 1: Retrieve the associated record First, you need to retrieve the record that has the Active Storage attachments. For example, if you have a model called "User" with Active Storage attachments, you can retrieve a specific user by its ID:

user = User.find(id)

Step 2: Access the attachments Once you have the user object, you can access its attachments using the user.attachments method. This will return an instance of ActiveStorage::Attached::Many that represents the attachments associated with the user:

attachments = user.attachments

Step 3: Get the attachment names To get the names of all the attachments, you can use the blobs method on the attachments object. This will give you an array of ActiveStorage::Blob objects representing the attached files:

blob_objects = attachments.blobs

Step 4: Extract the names Finally, you can extract the names of the attachments from the blob_objects array using the filename method. This will give you an array of attachment names:

attachment_names = blob_objects.map(&:filename)

Now, the attachment_names array will contain the names of all the attachments associated with the user.

Please note that you will need to replace "User" with the actual name of your model, and "attachments" with the name of your Active Storage attachment association.