Can i put more than one image on paperclip model

47 views Asked by At

Can i put more than one image on paperclip model? and how should i put it ?

rails g migration add_image_to_model_name

    def self.down
        remove_attachment :model_name, :image, :image2
      end

  def self.up
    remove_attachment :model_name, :image, :image2
  end

end

1

There are 1 answers

0
RAJ On

Best way is to create new table for paperclip attachments and setup has-many association between this table and your parent/existing table.

With this approach, you can upload images as many you need.

# app/models/gallery.rb
class Gallery < ActiveRecord::Base
  has_many :pictures, :dependent => :destroy
end

Create picture model and migration file, then define paperclip's has_attached_file in picture model.

# app/models/picture.rb
class Picture < ActiveRecord::Base
  belongs_to :gallery

  has_attached_file :image,
    :path => ":rails_root/public/images/:id/:filename",
    :url  => "/images/:id/:filename"
end

Here is a tutorial you can refer to: Adding multiple images to a Rails model with paperclip