Rails ActiveRecord Data Types
Tags: ruby, rails, database
Published: 2021-07-27 00:23:38

I usually curios to know about **Data Types for ActiveRecord**, but the [Active Record Migration](https://guides.rubyonrails.org/active_record_migrations.html) documentation itself does not have that information.

So, I do some Google search and here is the TL;DR.

### The ActiveRecord data types available in Rails 6;

- `:primary_key`
- `:string`: **short text**
- `:text`: **long text**
- `:integer`: **whole numbers** [-4, 0, 9, 772]
- `:float`: **double-precision floating-point numbers** [3244.90]
- `:decimal`: **high-precision floating-point numbers** [3244.2342343789212]
- `:datetime`
- `:time`
- `:date`
- `:binary`: **1 / 0**
- `:boolean`: **true or false**

These data types are used in instances such as **migrations**.

```ruby
def change
  create_table :categories do |t|
    t.string :title
    t.boolean :is_subcategory
    t.string :permalink
    t.timestamps
  end
end
```

---

## Specific DBMS Data Type

[**MySQL** Data Types](https://github.com/rails/rails/blob/25f6a8e84fb6c27f489fc3e593656ddb72f25613/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L16)

```ruby
NATIVE_DATABASE_TYPES = {
        primary_key: "bigint auto_increment PRIMARY KEY",
        string:      { name: "varchar", limit: 255 },
        text:        { name: "text" },
        integer:     { name: "int", limit: 4 },
        float:       { name: "float", limit: 24 },
        decimal:     { name: "decimal" },
        datetime:    { name: "datetime" },
        timestamp:   { name: "timestamp" },
        time:        { name: "time" },
        date:        { name: "date" },
        binary:      { name: "blob" },
        blob:        { name: "blob" },
        boolean:     { name: "tinyint", limit: 1 },
        json:        { name: "json" },
      }
```

[**PostgreSQL** Data Types](https://github.com/rails/rails/blob/25f6a8e84fb6c27f489fc3e593656ddb72f25613/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L119)

```ruby
NATIVE_DATABASE_TYPES = {
        primary_key: "bigserial primary key",
        string:      { name: "character varying" },
        text:        { name: "text" },
        integer:     { name: "integer", limit: 4 },
        float:       { name: "float" },
        decimal:     { name: "decimal" },
        datetime:    {}, # set dynamically based on datetime_type
        timestamp:   { name: "timestamp" },
        timestamptz: { name: "timestamptz" },
        time:        { name: "time" },
        date:        { name: "date" },
        daterange:   { name: "daterange" },
        numrange:    { name: "numrange" },
        tsrange:     { name: "tsrange" },
        tstzrange:   { name: "tstzrange" },
        int4range:   { name: "int4range" },
        int8range:   { name: "int8range" },
        binary:      { name: "bytea" },
        boolean:     { name: "boolean" },
        xml:         { name: "xml" },
        tsvector:    { name: "tsvector" },
        hstore:      { name: "hstore" },
        inet:        { name: "inet" },
        cidr:        { name: "cidr" },
        macaddr:     { name: "macaddr" },
        uuid:        { name: "uuid" },
        json:        { name: "json" },
        jsonb:       { name: "jsonb" },
        ltree:       { name: "ltree" },
        citext:      { name: "citext" },
        point:       { name: "point" },
        line:        { name: "line" },
        lseg:        { name: "lseg" },
        box:         { name: "box" },
        path:        { name: "path" },
        polygon:     { name: "polygon" },
        circle:      { name: "circle" },
        bit:         { name: "bit" },
        bit_varying: { name: "bit varying" },
        money:       { name: "money" },
        interval:    { name: "interval" },
        oid:         { name: "oid" },
      }
```

---

### [Rails data types mapping to different DB data types:](https://stackoverflow.com/questions/17918117/rails-4-list-of-available-datatypes/25702629#25702629)

It is important to know not only the types but the mapping of these types to the database types, too:

![Data type mapping 1](https://github.com/ifyouseewendy/ifyouseewendy.github.io/raw/source/image-repo/data_types_1.png)
![Data type mapping 2](https://github.com/ifyouseewendy/ifyouseewendy.github.io/raw/source/image-repo/data_types_2.png)

---

### The easy way ✌️

You can access this list everytime you want (even if you don't have Internet access) through:

```ruby
rails generate model -h
```

The end

---

resources;

[1](https://michaelsoolee.com/rails-activerecord-data-types/) [2](https://blog.corsego.com/rails-activerecord-data-types) [3](https://blog.ifyouseewendy.com/blog/2015/08/10/data-types-in-rails/) [4](https://stackoverflow.com/questions/17918117/rails-4-list-of-available-datatypes) [5](https://stackoverflow.com/questions/3956186/where-is-the-documentation-page-for-activerecord-data-types) [6](https://edgeguides.rubyonrails.org/active_record_postgresql.html) [7](https://stackoverflow.com/a/30132729/12595487)