Note that there are some explanatory texts on larger screens.

plurals
  1. POPaperclip :style depending on model (has_many polymorphic images)
    primarykey
    data
    text
    <p>I have set up my models to use a polymorphic Image model. This is working fine, however I am wondering if it is possible to change the :styles setting for each model. Found some examples using STI (Model &lt; Image) However this is not an option for me, because I am using a has_many relation.</p> <p>Art</p> <pre><code>has_many :images, :as =&gt; :imageable </code></pre> <p>Image</p> <pre><code>belongs_to :imageable, :polymorphic =&gt; true has_attached_file :file, :styles =&gt; { :thumb =&gt; "150x150&gt;", :normal =&gt; "492x600&gt;"} #Change this setting depending on model </code></pre> <h2>UPDATE</h2> <p>I tried starting up a debugger inside the Proc method. Only fields related to the attached file is populated:</p> <pre><code>run'irb(Image):006:0&gt; a.instance =&gt; #&lt;Image id: nil, created_at: nil, updated_at: nil, imageable_id: nil, imageable_type: nil, file_file_name: "IMG_9834.JPG", file_content_type: "image/jpeg", file_file_size: 151326, file_updated_at: "2010-10-30 08:40:23"&gt; </code></pre> <p>This is the object from ImageController#create</p> <pre><code>ImageController#create @image =&gt; #&lt;Image id: nil, created_at: nil, updated_at: nil, imageable_id: 83, imageable_type: "Art", file_file_name: "IMG_9834.JPG", file_content_type: "image/jpeg", file_file_size: 151326, file_updated_at: "2010-10-30 08:32:49"&gt; </code></pre> <p>I am using paperclip (2.3.5) and Rails 3.0.1. No matter what I do the a.instance object is the image with only the fields related to the attachment populated. Any ideas?</p> <h2>UPDATE2</h2> <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 onlye see the Paperclip stuff and that's it.</p> <p>I got around this problem by presaving the image from the Image controller with a before filter - without the attachment</p> <pre><code> before_filter :presave_image, :only =&gt; :create ... private def presave_image if @image.id.nil? # Save if new record / Arts controller sets @image @image = Image.new(:imageable_type =&gt; params[:image][:imageable_type], :imageable_id =&gt; params[:image][:imageable_id]) @image.save(:validate =&gt; false) @image.file = params[:file] # Set to params[:image][:file] if you edit an image. end end </code></pre>
    singulars
    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.
 

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