Skip to content

Instantly share code, notes, and snippets.

@willnet
Last active May 13, 2022 02:42
Show Gist options
  • Save willnet/b4b5d80704fab02a5d1867687f6b1724 to your computer and use it in GitHub Desktop.
Save willnet/b4b5d80704fab02a5d1867687f6b1724 to your computer and use it in GitHub Desktop.
string型もしくはtext型なのに最大長のバリデーションをかけていないものを出力するスクリプト
ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |model|
string_or_text_columns = model.columns.select { |column| column.type == :string || column.type == :text }
columns_with_maximum_length_validation = model.validators.select {|v| v.is_a? ActiveRecord::Validations::LengthValidator }.select {|v| v.options[:maximum] || v.options[:within] }.map(&:attributes).flatten.uniq
string_or_text_columns.each do |column|
if columns_with_maximum_length_validation.include?(column.name.to_sym)
puts "#{model.name}##{column.name} is OK"
else
puts "#{model.name}##{column.name} is NG"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment