LoginSignup
14
13

More than 5 years have passed since last update.

ActiveRecordのデフォルトのインデックス名が長いのが気になる

Last updated at Posted at 2012-12-26

たとえばActiveRecordでusersテーブルのemailカラムにインデックスを張ると、デフォルトのインデックス名がindex_users_on_emailみたいな冗長な名前になるのがうざいのでデフォルトはもっとすっきりさせることにする。

config/initializers/ar_simple_index_name.rb
ActiveSupport.on_load :active_record do
  module ActiveRecord::ConnectionAdapters

    # simple index name
    module SchemaStatements
      def index_name_with_simple(table_name, options)
        if Hash === options && options[:column]
          Array.wrap(options[:column]) * '_and_'
        else
          index_name_without_simple(table_name, options)
        end 
      end 
      alias_method_chain :index_name, :simple
    end 

  end 
end

こうしとくとusersテーブルのemailカラムにインデックスを張ると、デフォルトのインデックス名はemailになって、tigerカラムとbunnyカラムの複合キーのデフォルトのインデックス名もtiger_and_bunnyとすっきりしていい感じになる。

14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13