method
column_exists?
![Moderate documentation Importance_2](https://d2vfyqvduarcvs.cloudfront.net/images/importance_2.png?1349367920)
v3.2.13 -
Show latest stable
-
2 notes -
Class: ActiveRecord::ConnectionAdapters::SchemaStatements
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-4)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (38)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (31)
- 7.1.3.2 (0)
- 7.1.3.4 (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
![Default_avatar_30](https://www.gravatar.com/avatar/16b8d429e0123bc50ba8ad27cdaa6ff4?default=http://apidock.com/images/default_avatar_30.png&size=30)
1 thank
Example
Check if id column exists in users table
ActiveRecord::Base.connection.column_exists?(:users, :id)
![Default_avatar_30](https://www.gravatar.com/avatar/4b27856db49b84de81487fedbf674d7e?default=http://apidock.com/images/default_avatar_30.png&size=30)
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')