4

I´ve been searching everywhere without result.

So I´m using Active Storage to handle my model images.

My product model is as follows:

class Product < ApplicationRecord
  has_one_attached :image

  def set_average_color
    image = MiniMagick::Image.open(Rails.application.routes.url_helpers.rails_blob_url(self.image, only_path: true))
    image.resize("1x1")
    red, blue, green = image.get_pixels[0][0]
    update(average_color: hex_value(red, blue, green))
  end

So basically I want to save an attribute average_color with the hex value of the average color of the image.

My problem is when trying to open the image from active storage with MiniMagick using rails_blob_url, i get the following error:

Errno::ENOENT: No such file or directory @ rb_sysopen

I´ve been trying to use rails_blob_pathaswell without result. (By the way, all my Product objects have an image attached).

Which would be the correct way to access the image attached from the model and pass it to miniMagick??

Bonus question: Is there a way to call set_average_coloreach time my image has changed??

Thanks a lot!!!

Milan
  • 75
  • 1
  • 4

1 Answers1

0
MiniMagick::Image.read(image.download)
thisismydesign
  • 21,553
  • 9
  • 123
  • 126