Preferred Rails syntax for adding an index?
What's the preferred syntax for ad开发者_如何转开发ding an index to a table in Rails via a migration?
The preferred syntax is:
add_index(table_name, column_names, options)
There is a full write up in the docs here. To find the relevant section, just do a 'Ctrl-F' (or equivalent on your OS) search for 'index'. The first result is in the section you want to read over.
You can add in index in a separate migration later on, as shown in this post. Its a bit dated, but the idea is still the same. The most common 'options' are :name
and :unique
, for example
{ :name => "users_name_index", :unique => true }
Note: curly brackets are important, as the options are a hash.
You can simply use the add_index command in your migration:
add_index(:table_name, :column_name)
精彩评论