[Rails] Boolean values

Scott Barron scott at elitists.net
Tue Feb 1 12:34:12 GMT 2005


Brian L. wrote:
> I usually use a tinyint(1) and then add a custom accessor to the model as such:
> 
> def bit_field
>     read_attribute('bit_field') == 1
> end

This should not be necessary.  AR will treat the tinyint as a bool and 
set up a query method as such:

CREATE TABLE recipes ...
   TINY INT(1) is_tasty DEFAULT 1,
...

r = Recipe.find 1
puts "yum yum" if r.is_tasty?

The query method is how you should access the boolean value, and the 
form helpers like check_box should understand this magically.  It's 
quite handy.

-Scott
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 253 bytes
Desc: OpenPGP digital signature
Url : http://one.textdrive.com/pipermail/rails/attachments/20050201/1aa19d08/signature.bin


More information about the Rails mailing list