ActiveRecord迁移后的括号[5.1]是什么,它如何工作? [英] What are the brackets [5.1] after ActiveRecord Migration and how does it work?

查看:70
本文介绍了ActiveRecord迁移后的括号[5.1]是什么,它如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 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引入的新迁移版本。该数字表示创建迁移所用的迁移版本,在本例中为<$ c $。 c> 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.

这是 def self的类函数。 ActiveRecord :: Migration 的[](版本),它调用 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::[]
  • ActiveRecord::Migration::Compatibility

这篇关于ActiveRecord迁移后的括号[5.1]是什么,它如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆