What are the Ruby on Rails column types?

Published in March 2020

According to Rails source code, these are the 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:

  • :primary_key - translates into whatever the primary key datatype your database of choice requires
  • :string - use for short strings such as names and emails
  • :text - use for long strings, normally captured by textarea
  • :integer - it's a whole number, eg. 0,12,12532
  • :float - decimal numbers stored with fixed floating point precision
  • :decimal - stored precision varies; good for exact math calculations
  • :datetime - both date and time
  • :timestamp - same as datetime; usually used for created_at/updated_at
  • :time - hours, minutes, seconds
  • :date - year, month, day
  • :binary - store images, videos and other files in their original format in chunks of data called blobs
  • :blob - similar to binary, contains the metadata about a file. Blobs are intended to be immutable
  • :boolean - self explanatory - true/false
  • :json - from Rails 5 onwards, you can natively store JSON values thanks to this Pull Request. A lot of people still use Postgres' jsonb datatype though.

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: []

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

Level up your web development skills

Get articles, guides and interviews right in your inbox. Join a community of fellow developers.

No spam. Unsubscribe at any time.

Full Stack Heroes logo

© 2020 Full Stack Heroes