0
votes

Is there any way to generate different image styles based on models in paperclip with polymorphic association.

I have

Picture Model:

class Picture
  .....
  belongs_to :imageable, :polymorphic=>true
  has_attached_file :local_image,
  :styles => lambda{ |f|
    ['image/jpeg', 'image/jpg','image/png', 'image/gif'].include?(f.content_type) ? {
      :thumb => "40x40>",
      :small => "115x115>", 
      :medium => '300x300>',
      :large => "500x500>" } : {}
  }
  ...
end

User Model:

class User
  has_many :pictures, :as=> :imageable
end

Group Model:

class Group
  has_many :pictures, :as=> :imageable 
end

I want thumb,small sizes only for user model and medium,large sizes only for group model. I have tried this https://leomayleomay.github.io/blog/2013/07/30/polymorphic-image-styles-using-paperclip/ but it does not work for new pictures, only works for existing pictures

1

1 Answers

0
votes
##show only thumb size image to user
##using first as has_many associations
u=User.find 1
u.pictures.first.local_image.url(:thumb)


##show all other remaining sizes to group..using first as has_many associations
g=Group.find 1
g.pictures.first.local_image.url(:small)
g.pictures.first.local_image.url(:medium)
g.pictures.first.local_image.url(:large)

refer this for more info