Migration and Enum w/ MySQL

I know its not kindly looked upon for using enum w/ mysql, but how would I do this using the rails migration? or is it even possible? I can't seem to find this on any sites/books about rails migration. I was simply wanting 2 options of 'Y','N'

Ah, but how I love RoR... today was my first 'real' day outside of tutorials. And glad to just be joining the group!

-Salo

Doesn't mysql have a boolean type?

Sadly it does not, and the MySQL way to do boolean is to use enumerate Y/N, or at least that’s the use I saw most suggested when I was first starting to learn to use MySQL databases. The easiest way to handle this would honestly be to convert your enum columns into single bit integers (Integer :size => 1). But this may not be an option to you depending on who controls the data. Your only other option, I believe, would be to either write, or find, a ror to mysql driver that converts all boolean requests to the database from 1/0 to Y/N.

Booleans are a tricky thing to work with in RoR. The idea is to make your application database agnostic, but because of certain quirks, e.g. Oracle and mysql databases not having boolean options, it’s a little hard to reach RoR nirvana. The accepted practice seems to be to use single bit integers, which is why I suggested the change.

MySQL doesn’t have a native boolean type, but it will silently convert columns declared boolean to tinyint(1), which Rails recognizes as de-facto boolean.

jeremy

Makes sense to me to do any required conversion in the connection adapter - also explains why check_box returns 0 and 1 instead of the (to me) more intuitive true/false.

check the API, but I believe that you can specify, for checkboxes, what value it takes in and returns. check_box(object_name, method, options = {}, checked_value = “1”, unchecked_value = “0”)

as you can see, you can change the checked_value and unchecked_value to work with your interface. So you could create your checkboxes using:

check_box(model, method, {}, “Y”, “N”)

I'll give that a go right now. It would be nice if rails could detect true / false with the options instead of declaring 1=true 0=false (or maybe it can and I don't know about it :slight_smile:

Thanks for the input...

My team had some issues with this that you might be interested in. The short version: enums and migrations don't mix well at all - avoid.

long version:

- rob

It's funny that I found your site with that information on google before you posted here...

I'm starting to do just that: avoid enums and migrations, but I feel that this is something that should change in the future of RoR.

-Salo