我写了这个rake任务来查找Rails应用程序中未使用的列:
desc 'list all nil columns'
task list_empty_columns: :environment do
ActiveRecord::Base.connection.tables.each do |table|
# ignore some tables
next if %w[
action active blazer brands_templates brands_ categories_ components_ covers_ metadata punches schema silos tag
invoice session template_cate
].any? { |w| table.include?(w) }
# constantize the class name
klass = table.singularize.camelize.constantize
klass.columns.each do |column|
# check if all values are nil (problem must be here)
next unless klass.where(column.name => nil).all?
p "#{table} #{column.name} is all nil"
end
end
end
不幸的是,它会打印数据库中也有数据的列。这个任务有什么不对吗?
1条答案
按热度按时间rseugnpd1#
我会将其更改为: