Dan's Tech (and more) Blog
Some stuff about Active Record

Some stuff about Active Record

July 13, 2020 ( last updated : July 13, 2020 )
ruby active-record


1. Active Record uses a column called type for object inheritance

Taken from the docs:

Active Record allows inheritance by storing the name of the class in a column that by default is named “type” (can be changed by overwriting Base.inheritance_column)

For example, when you have classes that look like this:

class Person < ActiveRecord::Base; end

class Child < Person; end

and create a child object

Child.create(name: 'Billy')

Active Record stores the value "Child" in billy’s type column. Then we can retrieve Billy

Person.where(name: 'Billy')

Active Record will use the value in type to return an object of type Child.

2. The method remove_column needs a type to be used in a change block.

The method signature for Active Record’s remove_column method looks like this:

remove_column(table_name, column_name, type = nil, options = {})

type and options are optional params that will be added to the add_column call if the migration is reversed. Neat.