t56k
t56k

Reputation: 6981

Rails: turn existing Paperclip attachments into Image class

If I have lots of different kinds of Paperclip-attached images spread over several different models in a production environment, and I want to remove the attachments from those models and create an Image class that can have a relationship to those models, how can I do that?

Like, I can create the Image class and all, but how can I best translate the attribute-based solution into a relationship-based one so I don't lose existing images for specific records?

All advice welcomed. Thanks.

Upvotes: 0

Views: 144

Answers (1)

Anthony
Anthony

Reputation: 15967

You can do it right in your migration. Say users have an avatar:

class CreateImage < ActiveRecord::Migration
  def change
    create_table :images do |t|
      t.string 'url'
      t.integer 'user_id'
    end

    User.all.each do |user|
      Image.create(user: user, url: user.avatar)
    end
  end
end

Upvotes: 1

Related Questions