What are the Ruby on Rails column types?

Last updated on February 12, 2022

According to Rails source code, these are the native database types:

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" },
}

Let’s add more detail:

Array columns (if using Postgres)

Array is not a native database type in Rails, but if you want to add an array as a column, you can do the following migration

def change
  add_column :users, :locations, :text, array: true, default: []
end

This uses the Postgres datatype.

Other Postgres datatypes you can use

Besides arrays, you can add other Postgres datatypes, such as jsonb, in your Rails app. Rails has an entire article which covers this topic.

List the types of columns in your model

I came across this article which introduced me to #columns_hash. This method loads the schema and returns useful information about your model columns

> User.columns_hash["email"]

=> #<ActiveRecord::ConnectionAdapters::PostgreSQL::Column:0x00007fa7b42b1840 @name="email", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007fa7b42b1958 @sql_type="character varying", @type=:string, @limit=nil, @precision=nil, @scale=nil>, @null=true, @default=nil, @default_function=nil, @collation=nil, @comment=nil, @serial=nil

We can loop through them to get a list for our model

> User.columns_hash.each{|k,v| puts "#{k} is #{v.type} type"}

=> id is integer type
=> email is string type
=> encrypted_password is string type
...

Invite us to your inbox.

Articles, guides and interviews about web development and career progression.

Max 1-2x times per month.