Rails / Ruby – Check db for table or column

Does table exist

def self.table_exists?(name)
ActiveRecord::Base.connection.tables.include?(name)
end

Does column exist

if table_exists?(:profile) && !TableName.column_names.include?("url")
add_column :profile, :url, :string
end

Leave a Reply

Your email address will not be published. Required fields are marked *