donkey
donkey

Reputation: 4363

How to destroy columns mistakenly created by Paperclip in Ruby on Rails

As the title suggests, I accidentally add some random attachment columns to my model. Say I did rails generate paperclip portfolio href

How do I remove those columns created? rails destroy paperclip portfolio href doesnt seem to work!

Upvotes: 9

Views: 4528

Answers (6)

abdimuna
abdimuna

Reputation: 785

For recent version rails 4++
Just create a migration
Example:

rails g migration remove_attachment_photo_to_course
Then open the migration file

class RemoveAttachmentPhotoToCourse < ActiveRecord::Migration                                                                                                                                                                             
  def change                                                                                                                                                                                                                                   
    remove_attachment :courses, :photo                                                                                                                                                                                                   
  end                                                                                                                                                                                                                              
end

Then run, to update the table

rake db:migrate 

Where:
photo is the name of paperclip attachment you want to remove
courses is the name of table name

Upvotes: 8

Magne
Magne

Reputation: 17243

This will rollback the latest changes to the DB schema:

rake db:rollback

After that, simply delete the migration file.

This is presuming that you didn't commit any of the changes/migrations to a shared repository.

Upvotes: 1

Kevin R.
Kevin R.

Reputation: 1313

This comes from the Paperclip documentation:

remove_attachment :portfolios, :href

Upvotes: 1

Chloe
Chloe

Reputation: 26274

Normally for a migration you run rake db:rollback. Since this was a generator, you can just manually edit the table and drop the columns.

alter table portfolio drop column href_content_type;
alter table portfolio drop column href_file_name;
-- ... etc for each of the Paperclip columns.

You could also create a migration to drop the columns, but that would be overkill unless your whole team also ran the generator or you checked in schema.rb and others ran it also.

Here is an example migration for removing columns:

class RemoveMagazineFromBooks < ActiveRecord::Migration
  def change
    # This is not used ANYWHERE...
    if column_exists? :refinery_books, :magazine
      remove_column :refinery_books, :magazine
      remove_index :refinery_books, :magazine if index_exists? :refinery_books, :magazine
    end
  end
end

Notice the test to check if the column is there. Some people may not have run the previous migration and will not have the column.

Upvotes: 1

Michael Pell
Michael Pell

Reputation: 1476

$ rails generate migration RemoveColumnsFromModel portfolio href

should make you a migration that will remove two columns "href" and "portfolio" from some model's table. The migration will look something like:

class RemoveColumnsFromUser < ActiveRecord::Migration
  def change
    remove_column :users, :foo, :string
    remove_column :users, :bar, :string
  end
end

Upvotes: 0

donkey
donkey

Reputation: 4363

I have created a migration file and change the content to:

class RemoveAttachmentHrefToPortfolios < ActiveRecord::Migration   
  def self.up
    remove_attachment :portfolios, :href   
  end

  def self.down
    remove_attachment :portfolios, :href   
  end 
end

The problem is it is not the elegant and correct way to do it. I hope someone can improve this answer..

Upvotes: 6

Related Questions