Rails 4: List of available datatypes

334,846

Solution 1

Here are all the Rails 4 (ActiveRecord migration) datatypes:

  • :binary
  • :boolean
  • :date
  • :datetime
  • :decimal
  • :float
  • :integer
  • :bigint
  • :primary_key
  • :references
  • :string
  • :text
  • :time
  • :timestamp

Source: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_column
These are the same as with Rails 3.

If you use PostgreSQL, you can also take advantage of these:

  • :hstore
  • :json
  • :jsonb
  • :array
  • :cidr_address
  • :ip_address
  • :mac_address

They are stored as strings if you run your app with a not-PostgreSQL database.

More PostgreSQL data types

Solution 2

You might also find it useful to know generally what these data types are used for:

There's also references used to create associations. But, I'm not sure this is an actual data type.

New Rails 4 datatypes available in PostgreSQL:

  • :hstore - storing key/value pairs within a single value (learn more about this new data type)
  • :array - an arrangement of numbers or strings in a particular row (learn more about it and see examples)
  • :cidr_address - used for IPv4 or IPv6 host addresses
  • :inet_address - used for IPv4 or IPv6 host addresses, same as cidr_address but it also accepts values with nonzero bits to the right of the netmask
  • :mac_address - used for MAC host addresses

Learn more about the address datatypes here and here.

Also, here's the official guide on migrations: http://edgeguides.rubyonrails.org/migrations.html

Solution 3

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

enter image description here

enter image description here


Source added - Agile Web Development with Rails 4

Solution 4

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

rails generate model -h

Solution 5

Rails4 has some added datatypes for Postgres.

For example, railscast #400 names two of them:

Rails 4 has support for native datatypes in Postgres and we’ll show two of these here, although a lot more are supported: array and hstore. We can store arrays in a string-type column and specify the type for hstore.

Besides, you can also use cidr, inet and macaddr. For more information:

https://blog.engineyard.com/2013/new-in-rails-4

Share:
334,846
Nicolas Raoul
Author by

Nicolas Raoul

I am Nicolas Raoul, IT consultant in Tokyo. Feel free to copy/paste the source code from my StackExchange answers, I release it to the public domain.

Updated on July 08, 2022

Comments

  • Nicolas Raoul
    Nicolas Raoul almost 2 years

    Where can I find a list of data types that can be used in Ruby on Rails 4? Such as

    • text
    • string
    • integer
    • float
    • date

    I keep learning about new ones and I'd love to have a list I could easily refer to.

    • Mark Thomas
      Mark Thomas about 10 years
    • Nicolas Raoul
      Nicolas Raoul about 10 years
      @MarkThomas: Not a duplicate. My question here is specifically for Rails 4.
    • Mark Thomas
      Mark Thomas about 10 years
      The accepted answer in that question is essentially the same as your accepted answer. Both point to the Rails 4 docs. Also, it's a moot point because ActiveRecord datatypes didn't change from Rails 3 to Rails 4.
    • Dty
      Dty about 10 years
      I for one did not know AR datatypes didn't change between rails 3 and 4 so I'm thankful this question/answer is here.
  • Althaf Hameez
    Althaf Hameez almost 11 years
    I believe these are the datatypes that are supported across all databases. However as Peter de Ridder mentions there are some datatypes like hstore which are still supported.
  • ahnbizcad
    ahnbizcad over 9 years
    Question: postgres documentation doesn't seem to have text data type. Yet, rails can still handle it? What goes on in the background?
  • ahnbizcad
    ahnbizcad over 9 years
    Applause. +1 for thoroughness, and anticipation of usage. That's UX mentality right there.
  • makhan
    makhan over 9 years
    PostgreSQL does have a text datatype. Under the hood all varchar/text fields are variable length arrays. postgresql.org/docs/9.3/interactive/datatype-character.html
  • nlh
    nlh over 9 years
    Absolutely terrific answer - many thanks for this. The links to the articles on differences literally took the questions right out of my mouth.
  • Freedom_Ben
    Freedom_Ben over 9 years
    I would be interested to see the json type in this table, if you feel up to it
  • gotqn
    gotqn over 9 years
    You can always check the NATIVE_DATABASE_TYPES for the adapter you need - github.com/rails/rails/blob/master/activerecord/lib/…
  • bpercevic
    bpercevic about 9 years
    I'd like to add that if you're using a non-postgres database and your application schema_format is not set to use :sql, then your schema.rb file wont be able to dump the table that uses types like :json. The schema will still be dumped for the tables that use default types but you'll see a comment for the table with special types like, "could not dump table...". Look here to set the schema_format.
  • bpercevic
    bpercevic about 9 years
    Also, those columns will have type nil in a non-postgres database. You can inspect the type in the console with Model.columns_hash["column_name"].type. These are just things that I've run into when using :json column type, I may be wrong and this may not happen to everyone but I thought I'd let future readers know in case they have troubles. Regardless, +1 for this answer because it really helped me.
  • Robbie Guilfoyle
    Robbie Guilfoyle about 9 years
    I would also like to add :jsonb. Difference is here: "The json data type stores an exact copy of the input text, which processing functions must reparse on each execution; while jsonb data is stored in a decomposed binary format that makes it slightly slower to input due to added conversion overhead, but significantly faster to process, since no reparsing is needed. jsonb also supports indexing, which can be a significant advantage." postgresql.org/docs/9.4/static/datatype-json.html
  • Hugo
    Hugo almost 9 years
    Is there a spot on the guides or a single source of truth for these things over time?
  • TNT
    TNT almost 9 years
    For Postgres there is additionally a uuid type which can be used as normal field like t.uuid :name... or as primary key like create_table :users, id: :uuid do... or e.g. t.primary_key :id, :uuid, :default => 'uuid_generate_v1()'
  • Kevin Walsh
    Kevin Walsh over 8 years
    Additional PostgreSQL types supported by Rails listed in the API docs for ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnMethods. Highlights include money, json, xml, daterange
  • Toby 1 Kenobi
    Toby 1 Kenobi over 7 years
    Rails 4 also has a :bigint type
  • Martin Sommer
    Martin Sommer almost 7 years
    Yes, uuid was added for Postgres, but it is not returned with an Active Record create(). Has that issue been fixed with Rails 5?
  • mcr
    mcr over 6 years
    It's not "inet_address", (now?), but "inet" in rails5.
  • yohanes
    yohanes almost 4 years
    at 'Available field types:' section