Skip to content

Instantly share code, notes, and snippets.

@edavis10
Created January 29, 2011 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edavis10/801368 to your computer and use it in GitHub Desktop.
Save edavis10/801368 to your computer and use it in GitHub Desktop.
class Alert < ActiveRecord::Base
def self.delete_stale
execute <<-EOS
DELETE FROM alerts
WHERE alerts.alertable_type = 'Post'
AND alerts.alertable_id IN (
SELECT id FROM posts WHERE deleted = 1
)
EOS
end
end
# Use normal testing code since this is just a unit test now...
class DeleteAlertsForDeletedPosts < ActiveRecord::Migration
def self.up
Alert.delete_stale
end
def self.down
say "Previous migration removed stale alerts, there is nothing to do in this .down"
end
end
@christianbradley
Copy link

My issue with this is pointed out below:
http://gem-session.com/2010/03/how-to-use-models-in-your-migrations-without-killing-kittens

Since the resolution would be to move the code to the migration itself (ie: by reopening the Alert model and adding the delete_stale method within your migration .rb) it seems like the test focus moves back from the model and into the migration.

I follow where you're going with this though, and still find that this gray area of data migration (and not schema change) is not a well defined best practice. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment