Skip to content

Instantly share code, notes, and snippets.

@electrum
Created February 21, 2011 19:10
Show Gist options
  • Select an option

  • Save electrum/837535 to your computer and use it in GitHub Desktop.

Select an option

Save electrum/837535 to your computer and use it in GitHub Desktop.
Validate ActiveRecord models
namespace :models do
desc 'Check models for problems'
task :check => :environment do
Dir.glob(RAILS_ROOT + '/app/models/**/*.rb').each do |f|
model = eval File.basename(f)[0..-4].camelize
next unless model.superclass == ActiveRecord::Base # TODO: handle inheritance
print "#{model.name}: "
if model.column_names.exclude? model.primary_key
abort "bad primary key: #{model.primary_key}"
end
model.reflect_on_all_associations.map {|i| i.name.to_s }.each do |association|
begin
eval "model.new.#{association}"
rescue NameError
abort "bad association: #{association}"
end
end
puts "ok"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment