Search code examples
mysqlruby-on-railsrails-migrations

Rails and MySQL: Wrong interpretation of boolean values


I'm using Rails 5.0 with MySQL as database.

I have an existing table called "users" in which I store whether a users likes to cook in the "likes_to_cook" column. This column is interpreted as boolean by Rails and that is the expected behaviour.

I added a new column "likes_hot_meals" via migration:

add_column :users, :likes_hot_meals, :boolean, null: false, default: true

The migration worked, the only problem I now have is that this new column is not interpreted as boolean but instead as an integer. This results is 1/0 in the interface and API instead of true/false and this is not what I want.

Here is what the columns in the database look like:

mysql> describe users;
+-----------------------------------------------------+--------------+------+-----+--------------+----------------+
| Field                                               | Type         | Null | Key | Default      | Extra          |
+-----------------------------------------------------+--------------+------+-----+--------------+----------------+
| likes_to_cook                                       | tinyint(1)   | NO   |     | 1            |                |
| likes_hot_meals                                     | tinyint(1)   | NO   |     | 1            |                |
+-----------------------------------------------------+--------------+------+-----+--------------+----------------+

Does anyone have an explanation why this happens and how it can be fixed? Would be greatly appreciated! Thanks.


Solution

  • Figured it out! I didn't restart the application after this change which caused the issue described above.

    A fine example of "Have you tried turning it off and on again?" It was a long day...