What are the brackets [5.1] after ActiveRecord Migration and how does it work? [duplicate]
Asked Answered
K

1

8

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

Kreisler answered 5/11, 2017 at 20:49 Comment(0)
K
9

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:

Kreisler answered 5/11, 2017 at 20:49 Comment(1)
Interesting. So it's a dynamic class reference.Autarch

© 2022 - 2024 — McMap. All rights reserved.