Skip to content

Instantly share code, notes, and snippets.

@jondahl
jondahl / invalid_records.rake
Created March 3, 2009 19:56
Rake task to look for all invalid ActiveRecord records in a Rails application
namespace :db do
namespace :data do
desc "Find all invalid ActiveRecord records"
task :invalid_records => :environment do
models = Object.subclasses_of(ActiveRecord::Base)
models.each do |model|
begin
model.all.each do |record|
if !record.valid?
puts "#{model} #{record.id} is invalid: #{record.errors.full_messages.to_sentence}"
@jondahl
jondahl / gist:91930
Created April 8, 2009 18:59
Rake task to anonymize data
#
# Copyright (c) 2009 Jonathan Dahl
#
# Released under the MIT license. See below for details.
#
namespace :db do
namespace :data do
desc "Anonymize sensitive information"
task :anonymize => :environment do
@jondahl
jondahl / has_many :dependent => :restrict
Created August 18, 2009 23:56
9 lines of code to add a :dependent => :restrict option to has_many (equivalent of "ON DELETE restrict")
when :restrict
method_name = "has_many_dependent_restrict_for_#{reflection.name}".to_sym
define_method(method_name) do
unless send(reflection.name).empty?
# change to named error...
raise ActiveRecord::StatementInvalid, "can't delete record because of dependent #{reflection.name}"
end
end
before_destroy method_name

Keybase proof

I hereby claim:

  • I am jondahl on github.
  • I am jondahl (https://keybase.io/jondahl) on keybase.
  • I have a public key ASB5sEzrI01_cIXCStV1CH84V067GeRHa6I2qK_aVCS1UAo

To claim this, I am signing this object: