开发者

How do I write a migration that will remove certain records from my database?

I have a Note model, with a note_type field. How do 开发者_如何学JAVAwrite a migration that will remove Note records from the database if the type is "short_note"?


The code itself is simple.

Note.delete_all :type => 'short_note'

(If notes have destroy callbacks, you'll need to run destroy_all instead. It's slower because they're deleted one-by-one, but can sometimes produce better data integrity.)

However, I imagine you're a bit more worried about the down migration than the up migration. It is an irreversible transformation by nature. The answer to that particular bit of the question is that your migration should raise an ActiveRecord::IrreversibleMigration exception.

However, whenever you write an irreversible migration, it's important to consider why you're doing it. Depending on your situation, maybe it's more appropriate to just run that particular command in the console upon deploy to production than to make that migration part of your application's very definition.


def self.up
  execute "DELETE FROM notes WHERE note_type = 'short_note'"
end

:-P

Just kidding. I'm sure you could do this:

Note.delete_all :note_type => 'short_note'


In the self.up:

Note.delete_all("type = 'short_note'");

or use destroy_all which will call the record's destroy method and callbacks (before_destroy and after_destroy):

Note.destroy_all("type = 'short_note'");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜