How to check if there is an index on a column through Rails console?

There is an index_exists? method on one of the ActiveRecord “connection adapters” classes.

You can use it like on of the following methods:

ActiveRecord::Migration.connection.index_exists? :images, :post_id
ActiveRecord::Base.connection.index_exists? :images, :post_id

If you know the name of the index, instead you’ll need to use index_name_exists?

ActiveRecord::Base.connection.index_name_exists? :images, :index_images_on_join_key

Leave a Comment