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.
Articles RSS Feed
Sorry, comments are closed for this article.