ActiveRecord Migration 之后的括号 [5.1] 是什么,它是如何工作的? [英] What are the brackets [5.1] after ActiveRecord Migration and how does it work?
问题描述
使用 bin/rails g migration CreateUser
生成新迁移时,第一行将如下所示:
When generating a new migration using bin/rails g migration CreateUser
the first line will look like this:
class CreateUser < ActiveRecord::Migration[5.1]
[5.1]
代表什么以及它是如何工作的?
What does the [5.1]
stand for and how does it work?
这是对 Rails 5 的 ActiveRecord::Migration 中的 [5.0] 是什么意思? 因为它没有解释这是如何合法的红宝石,也没有出现在使用 [5.1]
或 <代码>括号
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
推荐答案
这是 Rails 5 引入的新迁移版本.数字表示创建迁移时使用的迁移版本,在本例中为版本 5.1
并且应该与 Rails 版本 >= 5.0
一起使用.
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
.
这是ActiveRecord::Migration
的类函数def self.[](version)
,调用Compatibility.find(version)
,用于向后兼容.
This is a class function def self.[](version)
of the ActiveRecord::Migration
, which calls Compatibility.find(version)
and is used for backward compatibility.
这里是来自 GitHub 的代码参考:
Here are the code references from GitHub:
这篇关于ActiveRecord Migration 之后的括号 [5.1] 是什么,它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!