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

As the name implies, I accidentally add some random anchor columns to my model. Let's say i didrails generate paperclip portfolio href

How to remove these columns? rails destroy paperclip portfolio hrefdoesn't seem to work!

+4
source share
6 answers

I created a transfer file and changed the contents 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 that this is not an elegant and proper way to do this. Hope someone can improve this answer.

+5
source

You usually run for migration rake db:rollback. Since it was a generator, you can simply manually edit the table and delete 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.

, , schema.rb, .

:

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

, , . , , .

+1

Paperclip:

remove_attachment :portfolios, :href
+1

:

rake db:rollback

.

, / .

+1

4 ++

:

rails g migration remove_attachment_photo_to_course

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

,

rake db:migrate 

:
- ,
-

+1

$ rails generate migration RemoveColumnsFromModel portfolio href

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

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

All Articles