Caching problem with Proc.new in rails production environment

30 views
Skip to first unread message

Andreas

unread,
Nov 20, 2009, 4:27:28 AM11/20/09
to Paperclip Plugin
Dear Community,

I am using paperclip to upload and resize images. The
has_attached_file call in the model looks similar to the following:

has_attached_file :photo,
:styles => { :original => '250x250>',
:small => '50x50',
:custom => Proc.new { |instance| "#
{instance.photo_width}x#{instance.photo_height}>" } }


When I switch the rails environment to "production" in the webserver
(Apache), there seems to be a caching problem with the Proc.new.

If I set the class caching to false, everything works, but it is no
good idea to work without cching in production mode.

Is it possible, to write the Proc.new style as an own Paperclip
processor file? Could this help?
Please send me any ideas to this topic!

Many Thanks.

Andreas

Kris Leech

unread,
Nov 20, 2009, 10:03:34 AM11/20/09
to papercli...@googlegroups.com

Andreas

unread,
Nov 23, 2009, 12:20:22 PM11/23/09
to Paperclip Plugin
Hi Kris,

thanks for your answer.
Yes, this looks quite interesting. A workaround for this problem. I
will try it soon.

The real problem seems not to be with paperclip but with the way,
rails caches classes. It occurs as well in named_scopes with dynamic
parameters.

Best Regards

Andreas

Timmy Crawford

unread,
Nov 29, 2009, 4:15:56 PM11/29/09
to Paperclip Plugin
Andreas-
Any luck with this issue? I'm experiencing the same problem in my app
using Passenger/Apache.

Jonathan Yurek

unread,
Nov 30, 2009, 10:05:32 AM11/30/09
to papercli...@googlegroups.com
This is how this feature was intended to work. It was not supposed to
be run every time, it was only run once, when the attachment is
defined. Currently, if :styles is a Proc, it's run in
Paperclip::Attachment#initialize and then never again. It was not
intended that it runs each time something is assigned to the attachment.
> --
> Individuals over processes. Interactions over tools.
>
> Agile Rails training from thoughtbot, the makers of Paperclip,
> Clearance, Shoulda, & Factory Girl:
> http://thoughtbot.com/services/training
>
> The Paperclip group:
> http://groups.google.com/group/paperclip-plugin
>
> To post to this group, send email to
> papercli...@googlegroups.com
>
> To unsubscribe from this group, send email to
> paperclip-plug...@googlegroups.com

--
Jonathan Yurek, Founder and CTO
thoughtbot, inc.
organic brains. digital solutions.

617.482.1300 x114
http://www.thoughtbot.com/

Adam Grant

unread,
Nov 30, 2009, 1:30:24 PM11/30/09
to papercli...@googlegroups.com
It would seem that your :custom style does no resizing at all. In fact it seems to just store the image in it's full size, or am I misinterpreting your question?

If that is the case, then you don't need to define :original. It will automatically get set to the full image. What you really should have is this:

:style => {
   :medium => '250x250>',
   :small => '50x50'
}

That will store three images: original, medium, and small to your preferred storage location. Original is always there, and will default to an unprocessed file (unless you overwrite it like you did).

If you need to resize based on some other attributes (for example, if photo_height and photo_width were form inputs that the user could specify), then you need to build your own custom processor. See this as an example:

http://mdeering.com/posts/018-paperclip-processors-doing-so-much-more-with-your-attachment


Cheers,
Adam
--
Adam Grant
Lead Web Engineer
Telaeris, Inc.


Timmy Crawford

unread,
Nov 30, 2009, 4:58:52 PM11/30/09
to Paperclip Plugin
@Jonathan - Thanks for the reply. I believe my usage is within the
scope of what you have said above, as in when the paperclip object is
first being initialized, the proc should run and be used for the style
sizing.

So I have two different thumbnail dimensions based upon the parent
objects attributes, for simplicity sake, I have an "A" image size and
a "B" image size.

If a user creates a paperclip object of image size A, and then on the
next request attempts to create a new object of image size "B", the
style attributes of size A are cached/persisted.

Is this the proper behavior? The site users usually re-upload and on
the 2nd or 3rd try, all seems to be well.

TIA
Timmy

On Nov 30, 10:30 am, Adam Grant <adam.jgr...@gmail.com> wrote:
> It would seem that your :custom style does no resizing at all. In fact it
> seems to just store the image in it's full size, or am I misinterpreting
> your question?
>
> If that is the case, then you don't need to define :original. It will
> automatically get set to the full image. What you really should have is
> this:
>
> :style => {
>    :medium => '250x250>',
>    :small => '50x50'
>
> }
>
> That will store three images: original, medium, and small to your preferred
> storage location. Original is always there, and will default to an
> unprocessed file (unless you overwrite it like you did).
>
> If you need to resize based on some other attributes (for example, if
> photo_height and photo_width were form inputs that the user could specify),
> then you need to build your own custom processor. See this as an example:
>
> http://mdeering.com/posts/018-paperclip-processors-doing-so-much-more...
>
> Cheers,
> Adam
> --
> Adam Grant
> Lead Web Engineer
> Telaeris, Inc.
>
> > paperclip-plug...@googlegroups.com<paperclip-plugin%2Bunsu...@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages