Orthogonal Thought | Random musings from the creator of Cooking For Engineers and Lead Architect of Fanpop

AUTHORS

CATEGORIES

ARCHIVE

ACTIONS

MySQL and Ruby on Rails datatypes

Posted 29 June, 2007 at 12:36pm by Michael Chu
(Filed under: Ruby on Rails, Web 2.0)

One of the best features of Ruby on Rails is the ability to interact with database tables as objects using ActiveRecord. Sometimes documentation can be a bit spotty, but after searching for a little bit it's not hard to find the mapping from Rails Migration types to MySQL data types. I did find it more difficult to find out what happens if I already have an existing MySQL table. What Ruby class gets created with what MySQL column type? I didn't find it on the web, so I put together this list…

I added links to MySQL documentation for each of the MySql data types and links to the relevant Ruby class type to make this table a bit more useful.
MySQL Column Data Type to Ruby class type (when using ActiveRecord)

MySQL Data Type Empty Ruby Class Ruby Class
bigint NilClass Bignum
binary String String
blob String String
bool NilClass TrueClass
FalseClass
char String String
date NilClass Date
datetime NilClass Time
decimal NilClass Fixnum
double NilClass Float
enum String String
float NilClass Float
int NilClass Bignum
longblob String String
longtext String String
mediumblob String String
mediumint NilClass Fixnum
mediumtext String String
set NilClass String
smallint NilClass Fixnum
text String String
time NilClass Time
timestamp NilClass Time
tinyblob String String
tinyint NilClass Fixnum
tinyint(1) NilClass TrueClass
FalseClass
tinytext String String
varbinary String String
varchar String String
year NilClass String

For completeness sake, here's a summary of Rails migration types and their MySQL column data types.
Rails Migration Type to MySQL Column Data Type

Rails Migration Symbol MySQL Data Type
:binary blob
:boolean tinyint(1)
:date date
:datetime datetime
:decimal decimal
:float float
:integer int(11)
:string varchar(255)
:text text
:time time
:timestamp datetime

23 comments to MySQL and Ruby on Rails datatypes

Free as in Time » Blog Archive » RoR Tuesday 03/07/2007, July 11th, 2007 at 8:37 am:

  • […] MySQL and Ruby on Rails datatypes - Sweet resource. One of the best features of Ruby on Rails is the ability to interact with database tables as objects using ActiveRecord. Sometimes documentation can be a bit spotty, but after searching for a little bit it’s not hard to find the mapping from Rails Migration types to MySQL data types. I did find it more difficult to find out what happens if I already have an existing MySQL table. What Ruby class gets created with what MySQL column type? I didn’t find it on the web, so I put together this list… […]

viktor, August 30th, 2007 at 4:28 am:

  • This is nice, but it seems like a flaw that Ruby on rails is incapable of managing the "double" data type..

    I know that there is a patch avaliable: http://dev.rubyonrails.org/ticket/7594 .
    Will that ever be a part of the standard RoR package ?

Tipos de datos Ruby on Rails a MySQL « Leobrambilla Weblog, August 30th, 2007 at 1:31 pm:

  • […] Esta es una info que me costo un poco encontrar por eso la comparto. Son los datos a cargar en el Modelo generado por […]

links for 2008-03-24 « Web Flakes, March 23rd, 2008 at 8:17 pm:

  • […] MySQL and Ruby on Rails datatypes (tags: rails.datatypes) […]

digital subterfuge » Blog Archive » Apples and Oranges. Er, RoR and MySQL data types., May 12th, 2008 at 10:09 pm:

  • […] This site has a handy chart to tell you. […]

Anonymous, March 17th, 2009 at 7:57 am:

  • good job!

Migrar una base de datos localizada con Ruby on Rails « Constructor sobrecargado, August 30th, 2009 at 2:04 am:

  • […] MySQL and Ruby on Rails datatypes […]

TNT, October 25th, 2009 at 5:35 am:

  • VERY IMPORTANT FOR CURRENCIES AND SIMILIAR:

    The decimal type is only mapped to Fixnum if :scale => 0 (which is the default). But if :scale is > 0 decimal is mapped to BigDecimal which is what we need for computations without the Float rounding errors.

Flex For Rails Scaffold | Softarama Labs, October 11th, 2010 at 3:27 pm:

  • […] (data types with Ruby and mySQL) […]

Visoft, Inc. Blogs | Rails 101 – Rails 01, April 24th, 2011 at 7:50 am:

  • […] http://www.orthogonalthought.com/blog/index.php/2007/06/mysql-and-ruby-on-rails-datatypes/ This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL. Damien White is a developer in Connecticut that simply loves coding. Ever since he was young, he has had a driving passion for computers and software development, and a thirst for knowledge that just cannot be quenched. He’s happy to share what he knows in his quest to learn as much as possible. « Entity Framework – Removing Foreign Keys from the Entity Model […]

Rails – Tipos de dados do ActiveRecord « RacionalRubyRails, May 19th, 2011 at 6:05 am:

How to: Rails 3 datatypes? | SevenNet, December 11th, 2014 at 6:37 am:

Answer: Rails 3 datatypes? #it #computers #programming | Good Answer, January 2nd, 2015 at 11:55 pm:

Solution: Rails 3 datatypes? #development #programming #it | IT Info, January 13th, 2015 at 8:26 am:

travel the world 2016, March 17th, 2016 at 4:09 am:

  • Thank you for some other magnificent post. Where else could
    anybody get that type of info in such an ideal approach of writing?
    I've a presentation next week, and I'm at the search for such info.

Noble, April 28th, 2016 at 12:32 am:

  • A physician will base a medical diagnosis on information you supply and on physical exam The characteristic appearance
    of the herpes sores leaves little doubt.

www.mycityfaces.com, April 28th, 2016 at 7:49 pm:

  • We focs on building and also mounting all your
    personalized shed kits, http://www.mycityfaces.com,
    as well as outdoors storage spae buildings in a variety of designs, designs and sizes to mattch any
    budget.

Adrianne, May 2nd, 2016 at 9:38 am:

  • When a patient has herpes, he can have one of 2 stress of the virus: type 1 or type 2.
    Not everyone who ends up being exposed to a herpes simplex virus will display
    signs.

Toko Online Terpercaya Di Indonesia, May 3rd, 2016 at 3:28 pm:

  • Oh my goodness! Impressive article dude! Thank you, However I am having troubles with your RSS.

    I don't understand why I am unable to join it.

    Is there anyone else getting similar RSS problems?
    Anyone that knows the answer will you kindly respond?
    Thanks!!

Shawn, July 20th, 2016 at 9:19 am:

  • Of course, to avoid the spread of the disease, infected
    persons experiencing an outbreak ought to likewise avoid contacting others with the
    infected area of skin through kissing or oral sex, and they
    should wash their hands with soap and warm water after touching any skin locations impacted by oral
    herpes.

howtoattractawoman16.beep.com, October 28th, 2016 at 5:04 am:

  • He doesn't do anything but sitting in the space
    thinking about his ex sweetheart who has actually already left him.

http://grandcru88.se/favicon.asp, October 29th, 2016 at 11:21 pm:

  • You actually make it appear really easy along with your presentation however I to find this matter to be really one thing which I think I would by no means understand. It kind of feels too complex and extremely huge for me. I am having a look forward for your next submit, Iˇll attempt to get the grasp of it!

Chloe, November 7th, 2016 at 12:10 am:

  • Small red bumps in the genital or anal area are the most typical sign of a herpes
    outbreak.

Your comment:

NAVIGATION

SEARCH