I have a model which stores user settings as 0 or 1 (for true/false). In my code, I’m having to do this:
if @user.settings.show_menu == 1
# do this
end
How can I leave out the == 1 or == 0? I’ve tried:
if @user.settings.show_menu
# do this
end
But it’s not evaluating as true, same with when using !@user.settings.show_menu
It’s been a long day, please guide me in the right direction. Thanks!
In Rails there is a
booleancolumn for use with the database that stores as a number and converts accordingly. Generally this is encoded in the database asSMALLINT. If you have a regularINTyou could always migrate to convert them.An example migration:
Within your app, the standard practice is to refer to boolean flags with their
?method version, like:If this method is not defined, you’ll get an exception which can lead you to discover the problem. This compares favorably to having it always evaluate as true.