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

6

9

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!

Denmark answered 20/8, 2014 at 1:19 Comment(2)
Does the generator create a migration file?Cnemis
Sure. But I can't revert this by using the destory counterpart.Denmark
K
8

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

Kohn answered 24/11, 2017 at 19:28 Comment(0)
D
6

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..

Denmark answered 20/8, 2014 at 1:31 Comment(0)
E
1

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.

Ellington answered 20/8, 2014 at 1:23 Comment(1)
It is a good solution! But I am afraid that I might need a solution using migration. I have came up with a solution. It is very dumb since i do not understand self.up and self.down. Do you mind giving me some advice over that?Denmark
O
1

This comes from the Paperclip documentation:

remove_attachment :portfolios, :href
Ostraw answered 15/9, 2016 at 7:45 Comment(0)
F
1

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.

Foetid answered 5/1, 2017 at 14:15 Comment(0)
C
0

$ 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
Cnemis answered 20/8, 2014 at 1:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.