Rails 3 Datatypes

Rails 3 datatypes?

Here are all the Rails3 (ActiveRecord migration) datatypes:

:binary

:boolean

:date

:datetime

:decimal

:float

:integer

:primary_key

:references

:string

:text

:time

:timestamp

Source

Rails 4: List of available datatypes

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

  • Rails 4
  • Rails 5
  • Rails 6
  • Rails 7

What is the difference between the Rails datatypes?

Here's a good set of definitions (from https://stackoverflow.com/a/15316528/2128691)

  • 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.
  • decimal - is for 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
  • 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.
  • time - is for time only
  • timestamp - for storing date and time into a column.

Where is the documentation page for ActiveRecord data types?

If you're talking about the types for migrations, e.g. string, integer, datetime, etc, then you want ActiveRecord::ConnectionAdapters::TableDefinition, the column method. (Rails 5 edit: see also connection.add_column.)

As of this update, the standard types are:

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

The implementation of :decimal is different with each database, so I'd avoid it if possible. You may use a type not in this list as long as it is supported by your database (for example, :polygon in MySQL), but this will not be database agnostic and should also be avoided.

SET data type in mysql with active record

No SET is not supported datatype in Rails.

Variable data type and changes to it

For the format of your 2-decimals number, see this post What is the best method of handling currency/money?

The best way to do is to not change it, and register it directly with rails.

If you have to change a string to a 2-decimal number, create a migration with

  • add_column my_decimal_number
  • execute an DB query to update your table from my_string_number to my_decimal_number
  • deletes my_string_number

How can I get the datatype of an attribute from a Rails model?

@model.column_for_attribute('title').type

That might be what you're looking for.



Related Topics



Leave a reply



Submit