A migration I created in a Rails 5 application had 5.0
passed into a method:
class CreateVariableKeys < ActiveRecord::Migration[5.0] ... end
I would like to know what the [5.0]
means.
A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.
Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.
Migrations are a convenient way to alter your database schema over time in a consistent way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent. You can think of each migration as being a new 'version' of the database.
Every time a migration is generated using the rails g migration command, Rails generates the migration file with a unique timestamp. The timestamp is in the format YYYYMMDDHHMMSS . Whenever a migration is run, Rails inserts the migration timestamp into an internal table schema_migrations .
It is a class method of ActiveRecord::Migration
and is defined here.
It allows us to select the version of migrations we wish to use between 4.2
and 5.0
. The method throws a:
"Unknown migration version ... "
error if an incompatible version is passed as an argument.
Production ready versions of ActiveRecord
don’t have that method so it should go away as soon as Rails 5 goes out of beta.
This blog has more info too
It seems to be there so that you don't have to upgrade old migrations, when moving from rails 4 to rails 5. (There are some small changes in the migrations API).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With