Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to setup Paperclip and associations for multiple models?
    text
    copied!<p>I have 3 models which should have images associated to them:</p> <pre><code>Program.rb Episode.rb Gallery.rb </code></pre> <p>Any of them needs to have different image sizes. For example:</p> <pre><code>Program -&gt; 100x100 , 250x400 , 500x400 Episode -&gt; 222x450 , 210x330 , 1000x1200 Gallery -&gt; 100x100 , 500x400 , 1000x1200 </code></pre> <p>Firstly, I thought a <code>polymorphic</code> association of a <code>Picture</code> model <code>has_attached_file</code> (Paperclip's helper) can be sufficient. Just like this:</p> <pre><code>class Picture &lt; ActiveRecord::Base has_many :imageable, :polypmorphic=&gt; :true has_attached_file :image, :styles =&gt; {... :program1 =&gt; "100x100&gt;", :program2 =&gt; "250x400&gt;", :episode1=&gt;"222x450" , :episode2=&gt;"210x330" , :gallery1=&gt;"100x100" , :gallery2=&gt;"500x400" .... } # There are almost 10 different styles for different models... end </code></pre> <p>By this way, I could use images just like this:</p> <pre><code>@program.pictures.first.image[:program1] @episode.pictures.second.image[:episode2] .. . </code></pre> <p>But I don't think that it's efficient? Is it?</p> <p><em>What is the best strategy to handle this kind of a need?</em></p> <p><em><strong>How should I build up my associations and Paperclip?</em></strong></p> <p><em><strong>For example, is it better to set paperclip for each of models?</em></strong></p> <p>I thought collecting all images inside one table would be a good practise?</p> <p>What do you think?</p> <p>This should be a common question in fact? Isn't it?</p> <p>Thank you</p>
 

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