Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am <em>really</em> late to the party here, but I wanted to clarify something about accessing model data for anyone else that happens on to this thread. I just faced this issue while using Paperclip to apply watermarks based on data from the model, and got it working after a lot of investigation.</p> <p>You said:</p> <blockquote> <p>After reading a lot on the Paperclip forum I don't believe it's possible to access the instance before it has been saved. You can only see the Paperclip stuff and that's it.</p> </blockquote> <p>In fact, you can see the model data <em>if it's been set in the object before your attachment is assigned</em>!</p> <p>Your paperclip processors and whatnot are invoked when the attachment in your model is assigned. If you rely on mass assignment (or not, actually), as soon as the attachment is assigned a value, paperclip does its thing.</p> <p>Here's how I solved the problem:</p> <p>In my model with the attachment (Photo), I made all the attributes EXCEPT the attachment <code>attr_accessible</code>, thereby keeping the attachment from being assigned during mass assignment.</p> <pre><code>class Photo &lt; ActiveRecord::Base attr_accessible :attribution, :latitude, :longitude, :activity_id, :seq_no, :approved, :caption has_attached_file :picture, ... ... end </code></pre> <p>In my controller's create method (for example) I pulled out the <code>picture</code> from the <code>params</code>, and then created the object. (It's probably not necessary to <em>remove</em> the picture from <code>params</code>, since the <code>attr_accessible</code> statement should prevent <code>picture</code> from being assgined, but it doesn't hurt). <em>Then</em> I assign the <code>picture</code> attribute, <em>after</em> all the other attributes of the photo object have been setup.</p> <pre><code>def create picture = params[:photo].delete(:picture) @photo = Photo.new(params[:photo]) @photo.picture = picture @photo.save ... end </code></pre> <p>In my case, one of the styles for picture calls for applying a watermark, which is the text string held in the <code>attribution</code> attribute. Before I made these code changes, the attribution string was never applied, and in the watermark code, <code>attachment.instance.attribution</code> was always <code>nil</code>. The changes summarized here made the entire model available inside the paperclip processors. <strong>The key is to assign your attachment attribute <em>last</em>.</strong></p> <p>Hope this helps someone.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload