# 1 vs True vs 't'

**URL:** <https://www.ruby-forum.com/t/1-vs-true-vs-t/159750>\
**Category:** Rails\
**Created:** [February 8, 2009, 8:20am UTC](https://www.ruby-forum.com/t/1-vs-true-vs-t/159750 "2009-02-08T08:20:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [February 8, 2009, 8:20am UTC](https://www.ruby-forum.com/t/1-vs-true-vs-t/159750/1 "2009-02-08T08:20:57Z")

</div>

I’m using SQLite3, but have been a longtime MySQL user. I have been  
wracking my brain over why I was getting a “0” result for a count when  
it should be “10”.

The statement…  
Table.count (:conditions =\> “approved = 0”)

The result in SQLite3: 0  
The result in MySQL: 10

The corrected (I think) statement for SQLite:  
Table.count (:conditions =\> "approved = ‘f’ ")

I am surprised and wonder why SQLite using the ‘f’ and ‘t’  
nomenclature when ‘0’ and ‘1’ have worked so well…

Since this specificity would rule out me easily switching my DB back  
to SQL, how can I write my command to use True and False?

Thanks a ton…

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [February 8, 2009, 8:22am UTC](https://www.ruby-forum.com/t/1-vs-true-vs-t/159750/2 "2009-02-08T08:22:42Z")

</div>

I did solve my problem…

Table.count (:conditions =\> {approved =\> false} )

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [February 8, 2009, 8:53am UTC](https://www.ruby-forum.com/t/1-vs-true-vs-t/159750/3 "2009-02-08T08:53:05Z")

</div>

yachtman wrote:

> I did solve my problem…
> 
> Table.count (:conditions =\> {approved =\> false} )

Or this will work as well:  
Table.count(:conditions =\> [“approved = ?”, true])

Putting the value in the conditions string doesn’t gives Rails a chance  
to process it. Instead it just goes directly into the WHERE clause as  
is. Both the hash version and the value substitution using the question  
mark version allow Rails to process and properly sanitize the SQL.

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [February 8, 2009, 8:54am UTC](https://www.ruby-forum.com/t/1-vs-true-vs-t/159750/4 "2009-02-08T08:54:15Z")

</div>

Robert W. wrote:

> Table.count(:conditions =\> [“approved = ?”, true])

Well I meant Table.count(:conditions =\> [“approved = ?”, false]), but  
you get the idea.

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [February 10, 2009, 3:53am UTC](https://www.ruby-forum.com/t/1-vs-true-vs-t/159750/5 "2009-02-10T03:53:31Z")

</div>

Robert,

Thanks for the reply.

I imagine in this case, since I am only counting based on a boolean  
value, there should be no worry about sanitization?

On Feb 8, 3:54 pm, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [February 10, 2009, 6:15am UTC](https://www.ruby-forum.com/t/1-vs-true-vs-t/159750/6 "2009-02-10T06:15:06Z")

</div>

On Feb 9, 2009, at 9:52 PM, yachtman wrote:

> > > Table.count(:conditions =\> [“approved = ?”, true])
> > 
> > ## Well I meant Table.count(:conditions =\> [“approved = ?”, false]), but you get the idea.

You’re not doing [‘approved = ?’,false] for the sanitization as much  
as for letting the database adapter fill in its own notion of what  
value it uses for false. Even if you were doing more than counting,  
you have control of the inputs so there’s no SQL injection here.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [February 10, 2009, 10:09am UTC](https://www.ruby-forum.com/t/1-vs-true-vs-t/159750/7 "2009-02-10T10:09:40Z")

</div>

seems to work. I think rails is doing its thing either way.

> > issue = Issue.count(:conditions =\> {:published =\> false})  
> > =\> 3

[4;36;1mSQL (0.000348) [0m [0;1mSELECT count(\*) AS count\_all FROM  
`issues` WHERE (`issues`.`published` = 0) [0m

On Mon, Feb 9, 2009 at 9:14 PM, Rob B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > wrote:  
> > as for letting the database adapter fill in its own notion of what

–  
Available for Ruby on Rails development opportunities
