Knock::on_wood

unclear thoughts on software development

Monday, December 10, 2007

Boolean Columns

Just found this little tidbit about returning a boolean from a tinyint column tonight on the rails wiki...

Columns which are either boolean or tinyint(1) are recognised as booleans by ActiveRecord. So, if you have a table “people” and a column “rocks tinyint(1)”, you can say:

person = People.find(1)
person.rocks = true

However, person.rocks will return an integer, and Ruby thinks that 0 is true. If you want to test for truth, use person.rocks?, like this view example:

<% if person.rocks? %>
  You rock, dude!
<% end %>

I did not know that. Pretty dang cool.

Sorry, comments are closed for this article.

Powered by Mephisto. Theme modified and ported by Ryan Wood, based on the WordPress theme minima.