Given there is a user with an attachable avatar:
class User < ApplicationRecord
  has_one_attached :avatar
end
You can copy the avatar from one user to another user with the code below:
user_1 = User.first
user_2 = User.create!(
  avatar: {
    io: StringIO.new(user_1.avatar.download),
    filename: user_1.avatar.blob.filename.to_s,
    content_type: user_1.avatar.blob.content_type.to_s,
  }
)
Note: For large attachments you might need to use a different approach to avoid memory issues.
Posted by Emanuel to makandra dev (2025-02-24 12:00)