Flowdock
method

column_exists?

Importance_2
column_exists?(table_name, column_name, type = nil, options = {}) public

Checks to see if a column exists in a given table.

Examples

# 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)
Show source
Register or log in to add new notes.
January 20, 2012
1 thank

Example

Check if id column exists in users table

ActiveRecord::Base.connection.column_exists?(:users, :id)
February 20, 2013
0 thanks

It also works with strings

It works with strings:

ActiveRecord::Base.connection.column_exists?('users', 'id')

Which is helpful if you need to specify the database/schema to use:

ActiveRecord::Base.connection.column_exists?('secondary.users', 'id')