问题
This question already has an answer here:
- What’s does the [5.0] in Rails 5’s ActiveRecord::Migration mean? 2 answers
When generating a new migration using bin/rails g migration CreateUser
the first line will look like this:
class CreateUser < ActiveRecord::Migration[5.1]
What does the [5.1]
stand for and how does it work?
This is a follow up on What’s does the [5.0] in Rails 5’s ActiveRecord::Migration mean? as it does not explain how this is legal ruby and does not show up in search using [5.1]
or brackets
回答1:
This is the new migration versioning introduced with Rails 5. The number indicates the migration version the migration was created with, in this case version 5.1
and should be used with Rails versions >= 5.0
.
This is a class function def self.[](version)
of the ActiveRecord::Migration
, which calls Compatibility.find(version)
and is used for backward compatibility.
Here are the code references from GitHub:
- ActiveRecord::Migration::[]
- ActiveRecord::Migration::Compatibility
来源:https://stackoverflow.com/questions/47126483/what-are-the-brackets-5-1-after-activerecord-migration-and-how-does-it-work