DEV Community

Ronak Bhatt
Ronak Bhatt

Posted on

Rails 6 - Active Storage changes

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 (variants).

In Rails 6, a new gem called image_processing is used by default to handle image variants. (commit)

The image_processing gem has below advantages:

  • New methods #resize_to_fit, #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. On libvips it doesn’t go above 1.20x slower, on average it’s only about 1.10x slower.

Fix for has_many_attached field in update query

Let’s say we have a User class and it has field images. Users can upload multiple images to their profiles. So we add has_many_attached method to User class as shown below

class User < ApplicationRecord
  has_many_attached :images
end
Enter fullscreen mode Exit fullscreen mode

update query replaces the existing collection instead of appending to the collection.

user.images.attach(filename: "profile_pic.jpg")

user.images.count
=> 1

blog = ActiveStorage::Blob.create_after_upload!(filename: "updated_pic.jpg")
user.update(images: [blog])

user.images.count
=> 1
user.images.first.filename
=> "updated_pic.jpg"
Enter fullscreen mode Exit fullscreen mode

Note:

  • We can append files by using the attach function.

  • More changes related to Active Storage can be found here.

Top comments (0)