简体   繁体   中英

Paperclip separate styles

How can i separate styles for different models?

I have my Photo model which generate my styles

large:'300x300#',huge:'800x800'

also i have two models witch use this styles

Product

and

Post

so i want to use

large style only for Product

and huge style only for Post

product => has_many :photos

post => has_one :photo

photo => belongs_to :post
      belongs_to :product
has_attached_file :image, :styles => {large:'300x300#',huge:'800x800'}

Is it possible?

I advice you to separte image by a image model type using STI, and polymorphism, therefore, add imageable_type , imageable_id , and type field to photo model, so:

app/models/photo.rb :

class Photo < AR::Base
   belongs_to :imageable, polymorphic: true
end

app/models/photos/large_photo.rb :

class LargePhoto < Photo
   has_attached_file :image, :styles => { large:'300x300#' }
end

app/models/photos/huge_photo.rb :

class HugePhoto < Photo
   has_attached_file :image, :styles => { huge:'800x800' }
end

app/models/product.rb :

class Product < AR::Base
   has_many :large_photos, as: imageable
end

app/models/post.rb :

class Post < AR::Base
   has_one :huge_photo, as: imageable
end

BUT for me, will be better to use carrierwave , rather than paperclip for this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM