There are cases where you may need to link images that are already stored in your Cloudinary account with a model record, for example, using the same image on multiple records, or copying from one model to another, without the need of reuploading the image.
The following demonstrates how to copy from one model to another:
if original_model.image_cloudinary.present?
image_cloudinary = Cloudinary::CarrierWave::StoredFile.new(original_model.image_cloudinary.identifier)
else
image_cloudinary = nil
end NewModel.create!(
image_cloudinary: image_cloudinary,
...
)
If you don't already have an old model with the identifier of the image, you can pass the info to a brand new model by creating an identifier with something like the following:
resource_type = "image"
type = "upload"
version = 1234567890
public_id = "myimage"
format = "jpg"
identifier = "#{resource_type}/#{type}/v#{version}/#{public_id}.#{format}"
NewModel.create!(
image_cloudinary:Cloudinary::CarrierWave::StoredFile.new(identifier)
...
)
As the same Cloudinary image may be associated with multiple model records now, then note that by default, the image will be deleted from Cloudinary whenever an associated model record is deleted. In other words, if the image is associated with model records A and B, then if A is deleted (even if B isn't), then the image will be deleted from Cloudinary and B will now point to a deleted image.
For more information about this and about how to override this behavior:
https://support.cloudinary.com/hc/en-us/articles/202520332-Why-is-my-image-accessible-in-a-certain-URL-and-not-in-a-different-one-
Comments
0 comments
Please sign in to leave a comment.