# Attr\_boolean, a good idea?

**URL:** <https://www.ruby-forum.com/t/attr-boolean-a-good-idea/64141>\
**Category:** Rails\
**Created:** [June 22, 2006, 12:39pm UTC](https://www.ruby-forum.com/t/attr-boolean-a-good-idea/64141 "2006-06-22T12:39:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [June 22, 2006, 12:39pm UTC](https://www.ruby-forum.com/t/attr-boolean-a-good-idea/64141/1 "2006-06-22T12:39:23Z")

</div>

Hello all,

I’ve recently been working with a model with a lot of boolean  
attributes and I decided that it’s worth trying to implement a  
attr\_boolean function to cut down on the work. Something like

class User \< ActiveRecord::Base  
attr\_boolean :contact\_by\_email, :display\_real\_name, :administrator  
end

which would replace the hand written

class User \< ActiveRecord::Base

```
def contact_by_email?
	self.contact_by_email == 1
     end

def display_real_name?
	self.contact_by_email == 1
     end

def administrator?
	self.contact_by_email == 1
     end

```

end

My questions are :

1. Does this facility already exist and I’ve just missed it?
2. Is it a good idea\worth the effort?
3. What’s the preferred way of representing a boolean in a database  
agnostic way? I usually just use a not null int field where !0 is true.

Many thanks,  
Gavin

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [June 22, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/attr-boolean-a-good-idea/64141/2 "2006-06-22T12:54:47Z")

</div>

Oops, in the hand-written example it should not, of course, read  
[self.contact\_by\_email ==1] inside every definition.

G

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [June 22, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/attr-boolean-a-good-idea/64141/3 "2006-06-22T12:58:04Z")

</div>

On 6/22/06, Gavin M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. Does this facility already exist and I’ve just missed it?

Yes. You can use model.attribute? to query an attribute. That  
functionality is built in and should do what you want.

> 1. Is it a good idea\worth the effort?

Unlikely, since Rails can already do this.

> 1. What’s the preferred way of representing a boolean in a database  
> agnostic way? I usually just use a not null int field where !0 is true.

I think that’s pretty standard. When you define your tables in a  
migration you simply define them as :boolean and it will map to the  
appropriate data type, usually just a integer.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [June 22, 2006, 1:07pm UTC](https://www.ruby-forum.com/t/attr-boolean-a-good-idea/64141/4 "2006-06-22T13:07:26Z")

</div>

Ah, I thought it seemed a bit to obvious to not have been  
implemented! I’m not sure how I missed it.

Many thanks,  
Gavin
