Dragonfly adding images to store before Model.save

480 views Asked by At

The Dragonfly doc implies that nothing is written to the datastore until the model is saved:

When the model is saved, a before_save callback persists the data to the App's configured datastore

Using Rails and Dragonfly 0.9.12, I have a model Article with image_accessor :image, and my store is S3. The following is sufficient to store an image in S3:

  1. Browser uploads image or provides image_url.
  2. New model instance: @article = Article.new(params[:article]))

That's it. If I add processing via after_assign :resize_image, then the processed version is stored.

Is this behavior intentional? It's causing stray images to be uploaded to S3. I don't always save @article to db, but I still need Dragonfly to resize the image. (If you're curious, I'm sending it back to the browser for a preview as embedded data using Base64.encode64(@article.image.data).)

To investigate, I disconnected my internet to interrupt the store operation. This is the relevant portion of the trace:

dragonfly (0.9.12) lib/dragonfly/active_model_extensions/attachment.rb:179:in `store_job!'
dragonfly (0.9.12) lib/dragonfly/active_model_extensions/attachment.rb:118:in `retain!'
dragonfly (0.9.12) lib/dragonfly/active_model_extensions/class_methods.rb:63:in `block (3 levels) in register_dragonfly_app'
mongoid (3.0.3) lib/mongoid/attributes/processing.rb:102:in `process_attribute'
mongoid (3.0.3) lib/mongoid/attributes/processing.rb:27:in `block in process_attributes'
mongoid (3.0.3) lib/mongoid/attributes/processing.rb:25:in `each_pair'
mongoid (3.0.3) lib/mongoid/attributes/processing.rb:25:in `process_attributes'
mongoid (3.0.3) lib/mongoid/document.rb:147:in `block in initialize'
mongoid (3.0.3) lib/mongoid/threaded/lifecycle.rb:84:in `_building'
mongoid (3.0.3) lib/mongoid/document.rb:142:in `initialize'

How can I get around this? Thanks.

1

There are 1 answers

0
enzo On

This is caused by using <%= f.hidden_field :retained_image %>.