Search code examples
ruby-on-railsrubyrmagick

rmagick stuck in an infinite loop that creates an image over and over


I'm using rmagick to create a montage. After I submit a request, it gets stuck in an infinite loop, and the request continues to create the same image over, and over, until I manually restart server:

class LineItem < ActiveRecord::Base
  has_many :images, as: :imageable, dependent: :destroy
  after_save :process

  private

  def process
    image_list = Magick::ImageList.new(*self.photos.split(','))
    montage = image_list.montage do
      self.geometry = "182x182+6+6"
      self.tile     = "4x3"
    end
    name = "#{self.id}_#{Time.now}.jpg"
    montage.write(name)
    images.build(source: File.open(name))
    save!
  end

Any idea why this is happening, or how to debug it?


Solution

  • Your problem is not in rmagick, but due to

    after_save :process
    

    then inside process

    save!
    

    which creates an infinite recursion.