jondahl jondahl

View has_many :dependent => :restrict
1 2 3 4 5 6 7 8 9
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
View gist:91930
1 2 3 4 5 6 7 8 9 10
#
# 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
View invalid_records.rake
1 2 3 4 5 6 7 8 9 10
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}"
Something went wrong with that request. Please try again.