method
column_exists?
v3.2.8 -
Show latest stable
-
2 notes -
Class: ActiveRecord::ConnectionAdapters::SchemaStatements
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0 (0)
- 3.0.5 (0)
- 3.0.7 (0)
- 3.0.9 (-38)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.3 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- What's this?
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)
Register or
log in
to add new notes.
stevo -
January 20, 2012
jonathonjones -
February 20, 2013
1 thank
Example
Check if id column exists in users table
ActiveRecord::Base.connection.column_exists?(:users, :id)
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')


