ruby Rake任务显示每个值都为nil的列

niknxzdl  于 2023-06-22  发布在  Ruby
关注(0)|答案(1)|浏览(103)

我写了这个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

不幸的是,它会打印数据库中也有数据的列。这个任务有什么不对吗?

rseugnpd

rseugnpd1#

next unless klass.where.(column.name => nil).all?

我会将其更改为:

相关问题