Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams 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. Mark Thomas Mar 30, 2014 at 19:00 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. Dty Apr 12, 2014 at 22:49
  • :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

  • Rails 4
  • Rails 5
  • Rails 6
  • Rails 7
  • 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. Althaf Hameez Jul 29, 2013 at 8:10 Question: postgres documentation doesn't seem to have text data type. Yet, rails can still handle it? What goes on in the background? ahnbizcad Aug 20, 2014 at 9:46 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 makhan Aug 22, 2014 at 4:59 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 Feb 18, 2015 at 19:19 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. bpercevic Feb 18, 2015 at 19:26
  • :string - is for small data types such as a title. ( Should you choose string or text? )
  • :text - is for longer pieces of textual data, such as a paragraph of information
  • :binary - is for storing data such as images, audio, or movies.
  • :boolean - is for storing true or false values.
  • :date - store only the date
  • :datetime - store the date and time into a column.
  • :time - is for time only
  • :timestamp - for storing date and time into a column.( What's the difference between datetime and timestamp? )
  • :decimal - is for decimals ( example of how to use decimals ).
  • :float - is for decimals. ( What's the difference between decimal and float? )
  • :integer - is for whole numbers.
  • :primary_key - unique key that can uniquely identify each row in a table
  • 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

    Applause. +1 for thoroughness, and anticipation of usage. That's UX mentality right there. ahnbizcad Aug 20, 2014 at 9:47 Absolutely terrific answer - many thanks for this. The links to the articles on differences literally took the questions right out of my mouth. nlh Sep 11, 2014 at 0:07 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()' TNT Jul 28, 2015 at 11:33 Additional PostgreSQL types supported by Rails listed in the API docs for ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnMethods . Highlights include money , json , xml , daterange Eliot Sykes Aug 21, 2015 at 15:09 Yes, uuid was added for Postgres, but it is not returned with an Active Record create(). Has that issue been fixed with Rails 5? Martin Sommer Jul 12, 2017 at 22:04 You can always check the NATIVE_DATABASE_TYPES for the adapter you need - github.com/rails/rails/blob/master/activerecord/lib/… gotqn Jan 22, 2015 at 21:11 date and datetime are mapped to date for oracle database. How does active record determine to which data type it should map when fetching? Ali Akbar Jan 3 at 11:14

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

    rails generate model -h
    

    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

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.