# Checking booleans while maintaining database compatibility

**URL:** <https://www.ruby-forum.com/t/checking-booleans-while-maintaining-database-compatibility/156954>\
**Category:** Rails\
**Created:** [January 5, 2009, 7:48pm UTC](https://www.ruby-forum.com/t/checking-booleans-while-maintaining-database-compatibility/156954 "2009-01-05T19:48:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 5, 2009, 7:48pm UTC](https://www.ruby-forum.com/t/checking-booleans-while-maintaining-database-compatibility/156954/1 "2009-01-05T19:48:16Z")

</div>

Users have many invitations and Invitations belongs\_to a User. In the  
user model:

has\_many :open\_invitations,  
:class\_name =\> ‘Invitation’,  
:conditions =\> “completed\_at IS NULL AND is\_closed = ‘f’”

This works for SQLite3. But as a literal string this will fail if the  
boolean values are different in another db system like MySQL, right?  
How can I use a boolean in a condition that will work across all db  
types?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 5, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/checking-booleans-while-maintaining-database-compatibility/156954/2 "2009-01-05T20:13:43Z")

</div>

Taylor S. wrote:

> has\_many :open\_invitations,  
> :class\_name =\> ‘Invitation’,  
> :conditions =\> “completed\_at IS NULL AND is\_closed = ‘f’”
> 
> This works for SQLite3. But as a literal string this will fail if the  
> boolean values are different in another db system like MySQL, right?  
> How can I use a boolean in a condition that will work across all db  
> types?

I have done things like [http://pastie.org/353140](http://pastie.org/353140)

Best Regards,

Danny

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 5, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/checking-booleans-while-maintaining-database-compatibility/156954/3 "2009-01-05T20:26:47Z")

</div>

On Mon, Jan 5, 2009 at 2:13 PM, Danny B. \<  
removed\_email\_address@domain.invalid\> wrote:

> > types?
> 
> I have done things like [http://pastie.org/353140](http://pastie.org/353140)

:conditions =\> [“completed\_at IS NULL AND is\_closed = ?”, false] works  
across databases when finding records. Does it work in a has\_many  
declaration?

has\_many :open\_invitations, :class\_name =\> …, :conditions =\>  
[“completed\_at IS NULL AND is\_closed = ?”, false]

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 5, 2009, 8:38pm UTC](https://www.ruby-forum.com/t/checking-booleans-while-maintaining-database-compatibility/156954/4 "2009-01-05T20:38:16Z")

</div>

Craig D. wrote:

> On Mon, Jan 5, 2009 at 2:13 PM, Danny B. \<  
> removed\_email\_address@domain.invalid\> wrote:
> 
> > > types?
> > 
> > I have done things like [http://pastie.org/353140](http://pastie.org/353140)
> 
> :conditions =\> [“completed\_at IS NULL AND is\_closed = ?”, false] works  
> across databases when finding records. Does it work in a has\_many  
> declaration?
> 
> has\_many :open\_invitations, :class\_name =\> …, :conditions =\>  
> [“completed\_at IS NULL AND is\_closed = ?”, false]
> 
> Regards,  
> Craig

Thanks for the replies! Craig’s solution worked - you CAN use a “safe  
array” in the conditions of has\_many. The final associations look like  
this and have been confirmed working in the console:

class User  
has\_many :invitations  
has\_many :open\_invitations,  
:class\_name =\> ‘Invitation’,  
:conditions =\> [“completed\_at IS NULL AND is\_closed = ?”,  
false]  
has\_many :closed\_invitations,  
:class\_name =\> ‘Invitation’,  
:conditions =\> [‘completed\_at IS NOT NULL OR is\_closed = ?’,  
true]

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 5, 2009, 8:39pm UTC](https://www.ruby-forum.com/t/checking-booleans-while-maintaining-database-compatibility/156954/5 "2009-01-05T20:39:26Z")

</div>

Craig D. wrote:

> :conditions =\> [“completed\_at IS NULL AND is\_closed = ?”, false] works  
> across databases when finding records. Does it work in a has\_many  
> declaration?

Excellent, thanks- more code I can eliminate 🙂

- D
