1
votes

I'm using a polymorphic Picture model to associate images with other models like User, Place etc.

As paperclip docs, Picture model can define a set of styles using:

has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }

But being a polymorphic and associated to different models the required styles per entry will be different. To solve this how can i set custom styles on the fly?

1

1 Answers

0
votes

We use something like what you're referring to.

--

We set the Paperclip defaults in the config/application.rb file:

#config/application.rb
...

config.paperclip_defaults = {
   styles: { :large => "x850", :medium => "x450", :thumb => "x200"},
   default_url: "layout/placeholders/:style/placeholder.png"
}

This sets a default set of styles which can be overridden in the model itself.

We then use an asset model (with corresponding DB) to save all the data. This asset model then delegates the url method (for Paperclip) to its dependent model.

enter image description here

enter image description here

enter image description here

enter image description here

The original code for the polymorphic association is here.

--

Because the Paperclip styles are defaults, you can override them as you need in any of your dependent models.