Active Storage was introduced in Rails 5.2. In Rails 6, there are enhancements done to Active Storage.
Let’s explore them.
mini_magick replaced by image_processing gem
In Rails 5.2, Active Storage was using
mini_magick to handle image resizing
image_processing gem has below advantages:
- New methods
#resize_to_fill, etc also sharpens the thumbnail after resizing.
- It fixes the image orientation automatically. This can be referred here.
- It provides another backend libvips
that has significantly better performance than ImageMagick.
With ImageMagick, resizing and sharpening a 1600x900 image to 800x800 is 1.87x slower,
and to 300x300 is 1.18x slower.
libvipsit doesn’t go above 1.20x slower, on average it’s only about 1.10x slower.
New image variants support
has_many_attached field in update query
Let’s say we have a
User class and it has field
Users can upload multiple images to their profiles.
So we add
has_many_attached method to
User class as shown below
Before Rails 6:
We attach an image to the user as shown below and verify the count
Now, when we update the
image field, the new image was getting appended to
This is not consistent with ActiveRecord
update, where it replaces the existing
value of a record.
In Rails 6:
update query replaces the existing collection instead of appending to
We can append files by using the
More changes related to Active Storage can be found here.