Uday's website
Published on Uday's website (https://uday.net)

Home > Rails - List of datatypes for ActiveRecord

Rails - List of datatypes for ActiveRecord [1]

Submitted by uday [2] on July 22, 2020 - 4:54pm
  • :string - is for small data types such as a title. (Should you choose string or text? [3])
  • :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? [4])
  • :decimal - is for decimals (example of how to use decimals [5]).
  • :float - is for decimals. Note: Never use float datatype for currency, use decimal. Basically, it stores sign, fraction and exponent to represent a float. Use float when you don't care about precision too much e.g. latitude/longitude values etc. Use decimal always, when precision is required. e.g. numbers that need to be precise and add up correctly (like compounding interest and currency related calculations)
  • :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 [6].

New Rails 4 datatypes available in PostgreSQL:

  • :hstore - storing key/value pairs within a single value (learn more about this new data type [7])
  • :array - an arrangement of numbers or strings in a particular row (learn more about it and see examples [8])
  • :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 [9] and here [10].

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

Useful link: https://stackoverflow.com/questions/17918117/rails-4-list-of-available-datatypes [12]

Rating: 
Tags: 
Rails [13]

 Valid XHTML 1.0 StrictCreative Commons LicenseVerbatim copying and redistribution of this entire page are permitted provided this notice is preserved. All content on this website including archives (including text, photographs, audio files and any other original work), unless otherwise noted, is licensed under a Creative Commons License or later.


Source URL:https://uday.net/rails-datatypes-for-ActiveRecord

Links
[1] https://uday.net/rails-datatypes-for-ActiveRecord [2] https://uday.net/user/1 [3] https://stackoverflow.com/questions/3354330/difference-between-string-and-text-in-rails [4] https://stackoverflow.com/questions/409286/datetime-vs-timestamp [5] https://stackoverflow.com/questions/813287/how-to-store-decimal-values-in-sql-server [6] https://stackoverflow.com/questions/6242100/what-is-and-examples-of-using-data-type-references [7] http://www.postgresql.org/docs/9.1/static/hstore.html [8] http://reefpoints.dockyard.com/ruby/2012/09/18/rails-4-sneak-peek-postgresql-array-support.html [9] http://reefpoints.dockyard.com/ruby/2012/05/18/rails-4-sneak-peek-expanded-activerecord-support-for-postgresql-datatype.html [10] http://blog.remarkablelabs.com/2012/12/a-love-affair-with-postgresql-rails-4-countdown-to-2013 [11] http://edgeguides.rubyonrails.org/migrations.html [12] https://stackoverflow.com/questions/17918117/rails-4-list-of-available-datatypes [13] https://uday.net/taxonomy/term/82