Want more GoRails?
GoRails is packed full with 688 lessons just like this one.
Sign up to get full access or log in to your account and sit back.
Your Teacher
Chris Oliver
Hi, I'm Chris. I'm the creator of GoRails, Hatchbox.io and Jumpstart. I spend my time creating tutorials and tools to help Ruby on Rails developers build apps better and faster.
About This Episode
Rails uses the Ruby subscript operator to implement versioning with ActiveRecord Migrations. We'll learn how this works and implement it from scratch to see how it all ties together
Notes
Resources
module ActiveRecord
class Migration
# Receives a version of ActiveRecord as a float
# Return a Class
def self.[](version)
class_name = "V#{version.to_s.gsub(".", "_")}" # => "V7_0"
ActiveRecord.const_get(class_name)
end
def add_column(table_name, column_name, column_type)
puts "Adding #{column_name} to #{table_name} table"
end
end
class V7_0 < Migration
def add_column(table_name, column_name, column_type)
puts "Adding #{column_name} to #{table_name} table with old version 7.0"
end
end
class V8_0 < Migration
end
end
class AddEmailToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :email, :string
end
end
class AddNameToUsers < ActiveRecord::Migration[8.0]
def change
add_column :users, :name, :string
end
end
AddEmailToUsers.new.change
AddNameToUsers.new.change
Ruby Language
0% Complete
1
How to use the Ruby 2.6 Just-in-Time (JIT) Compiler
6m
2
Ruby's Array() and Array.wrap
8m
3
Ruby Module Include Tracking
11m
4
Symbols vs Strings
10m
5
Wrapping Exceptions in Ruby
8m
6
How to use "ensure" in Ruby
8m
7
Flattening Scopes in Ruby
19m
8
Concept Application: Assert Difference Deep Dive
23m
9
Eigenclasses
15m
10
Your First Ruby Gem
21m
11
Your First Ruby Gem Part 2
24m
12
Syncing Repositories with Ruby Scripts - Part 1
33m
13
Building Utilities with Ruby Scripts
21m
14
Debugging with Caller
8m
The Ruby Subscript Operator
14m
16
Bundler's New Ruby Version File Option
4m
17
The Behaviors Of Super
13m
18
Linux Signal Trapping in Ruby
8m
19
Syncing Repositories with Ruby Scripts - Part 2
37m
20
What's New In Ruby 3.3
17m