# Conditions don't work

**URL:** <https://www.ruby-forum.com/t/conditions-dont-work/172026>\
**Category:** Rails\
**Created:** [July 13, 2009, 3:11pm UTC](https://www.ruby-forum.com/t/conditions-dont-work/172026 "2009-07-13T15:11:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![plplpl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@plplpl](https://www.ruby-forum.com/u/plplpl)\
**Post date:** [July 13, 2009, 3:11pm UTC](https://www.ruby-forum.com/t/conditions-dont-work/172026/1 "2009-07-13T15:11:08Z")

</div>

Hi there,

I’m building a private message system for one of my sites. I’m now  
trying to give users the delete messages in their inbox (not destroy  
the records but put “archived = true” in them), so they don’t come up  
anymore. This is important because the sender needs to have the  
message in his sent items.

Everything is set up but when I do the database query, it simply  
returns no messages.

This was my query before the archive feature and shows records:  
@messages = Message.find\_all\_by\_to(current\_user.id, :order =\>  
“created\_at DESC”)

And this is the query with archive feature and shows no records:  
@messages = Message.find\_all\_by\_to(current\_user.id, :conditions =\>  
{:archived, :order =\> “created\_at DESC”)

The strange thing is that Mongrel shows the right query:  
Message Load (0.6ms) SELECT \* FROM “messages” WHERE  
(“messages”.“archived” = ‘false’) AND (“messages”.“to” = 3) ORDER BY  
created\_at DESC

But I don’t see any messages anymore (archived and not archived).

Hope you can help me.

pl

---

<div class="post-metadata">

**Author:** ![plplpl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@plplpl](https://www.ruby-forum.com/u/plplpl)\
**Post date:** [July 13, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/conditions-dont-work/172026/2 "2009-07-13T15:23:59Z")

</div>

Hi pl,

On Mon, 2009-07-13 at 06:10 -0700, pl wrote:

> The strange thing is that Mongrel shows the right query:  
> Message Load (0.6ms) SELECT \* FROM “messages” WHERE  
> (“messages”.“archived” = ‘false’) AND (“messages”.“to” = 3) ORDER BY  
> created\_at DESC
> 
> But I don’t see any messages anymore (archived and not archived).

What does the database return if you execute the select outside of your  
app? (I’m assuming you either know the command-line syntax or have a  
GUI front-end.)

Bill

---

<div class="post-metadata">

**Author:** ![plplpl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@plplpl](https://www.ruby-forum.com/u/plplpl)\
**Post date:** [July 13, 2009, 3:47pm UTC](https://www.ruby-forum.com/t/conditions-dont-work/172026/3 "2009-07-13T15:47:22Z")

</div>

The problem is in the query:

sqlite\> SELECT \* FROM “messages” WHERE (“messages”.“to” = 3) ORDER BY  
created\_at DESC;  
23|Hoi Pepijn|7|3|true|2009-07-13 07:40:41|2009-07-13 13:13:45|t  
22|Hoi testje|7|3|true|2009-07-13 07:21:44|2009-07-13 07:33:48|f  
21|Hoi sjaak|7|3|true|2009-07-13 07:12:19|2009-07-13 07:36:59|f  
19|En nu dan?|7|3|true|2009-07-13 07:02:20|2009-07-13 07:34:59|f  
17|Heel misschien nu|7|3|true|2009-07-13 06:46:40|2009-07-13 07:35:09|  
f  
15|Hoi Pepi!|7|3|true|2009-07-13 06:40:30|2009-07-13 07:35:35|f  
sqlite\> SELECT \* FROM “messages” WHERE (“messages”.“archived” =  
‘false’) AND (“messages”.“to” = 3) ORDER BY created\_at DESC;  
sqlite\>

Suggestions for a better query? Is it an SQLite/MySQL difference?  
Never used SQLite before.

---

<div class="post-metadata">

**Author:** ![plplpl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@plplpl](https://www.ruby-forum.com/u/plplpl)\
**Post date:** [July 13, 2009, 4:59pm UTC](https://www.ruby-forum.com/t/conditions-dont-work/172026/4 "2009-07-13T16:59:50Z")

</div>

On Mon, 2009-07-13 at 06:46 -0700, pl wrote:

> 15|Hoi Pepi!|7|3|true|2009-07-13 06:40:30|2009-07-13 07:35:35|f  
> sqlite\> SELECT \* FROM “messages” WHERE (“messages”.“archived” =  
> ‘false’) AND (“messages”.“to” = 3) ORDER BY created\_at DESC;  
> sqlite\>
> 
> Suggestions for a better query? Is it an SQLite/MySQL difference?  
> Never used SQLite before.

If I’m reading the results above correctly, it looks like the ‘archived’  
field is the one at the end. Yes? If so, then the query needs to test  
for ‘f’ not ‘false’. If not, this it looks like the archived field must  
be the one just before the first date and you show no records with a  
value of ‘false’.

HTH,  
Bill
