instance method
column_exists?
Ruby on Rails 6.1.7.10
Since v3.0.20Signature
column_exists?(table_name, column_name, type = nil, **options)
Checks to see if a column exists in a given table.
# Check a column exists column_exists?(:suppliers, :name) # Check a column exists of a particular type column_exists?(:suppliers, :name, :string) # Check a column exists with a specific definition column_exists?(:suppliers, :name, :string, limit: 100) column_exists?(:suppliers, :name, :string, default: 'default') column_exists?(:suppliers, :name, :string, null: false) column_exists?(:suppliers, :tax, :decimal, precision: 8, scale: 2)
Parameters
-
table_namereq -
column_namereq -
typeopt = nil -
optionskeyrest
Source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 135
def column_exists?(table_name, column_name, type = nil, **options)
column_name = column_name.to_s
checks = []
checks << lambda { |c| c.name == column_name }
checks << lambda { |c| c.type == type.to_sym rescue nil } if type
column_options_keys.each do |attr|
checks << lambda { |c| c.send(attr) == options[attr] } if options.key?(attr)
end
columns(table_name).any? { |c| checks.all? { |check| check[c] } }
end
Defined in activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb line 135
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::ConnectionAdapters::SchemaStatements