# Conditions Issue

**URL:** <https://www.ruby-forum.com/t/conditions-issue/180360>\
**Category:** Rails\
**Created:** [December 18, 2009, 1:22pm UTC](https://www.ruby-forum.com/t/conditions-issue/180360 "2009-12-18T13:22:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 18, 2009, 1:22pm UTC](https://www.ruby-forum.com/t/conditions-issue/180360/1 "2009-12-18T13:22:05Z")

</div>

Controller:

> def demonstration\_domains  
> @projects = Project.find(:all, :conditions =\> :demo\_domain != nil)  
> end

View:

> \<% for project in @projects %\>
> 
> \<% end %\>

Those conditions do not seem to have any effect. Is there something I  
need to amend in my database for this to pass, or is there something  
else I’m possibly missing or doing incorrectly?

‘demo\_domain’ is a text column nulled by default in my database.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 18, 2009, 2:21pm UTC](https://www.ruby-forum.com/t/conditions-issue/180360/2 "2009-12-18T14:21:59Z")

</div>

Pale H. wrote:

> Controller:
> 
> > def demonstration\_domains  
> > @projects = Project.find(:all, :conditions =\> :demo\_domain != nil)  
> > end
> 
> View:
> 
> > \<% for project in @projects %\>
> > 
> > \<% end %\>
> 
> Those conditions do not seem to have any effect. Is there something I  
> need to amend in my database for this to pass, or is there something  
> else I’m possibly missing or doing incorrectly?
> 
> ‘demo\_domain’ is a text column nulled by default in my database.

I really need to ask : why the for loop instead of ‘each’ ?  
Also …  
@projects = Project.find(:all, :conditions =\> {:demo\_domain =\> !nil} )  
or maybe  
@projects = Project.find(:all, :conditions =\> [‘demo\_domain \<\> ?’, nil])

But, I think you get the idea.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 18, 2009, 3:08pm UTC](https://www.ruby-forum.com/t/conditions-issue/180360/3 "2009-12-18T15:08:50Z")

</div>

> I really need to ask : why the for loop instead of ‘each’ ?

Why not? It works perfectly.

> Also …  
> @projects = Project.find(:all, :conditions =\> {:demo\_domain =\> !nil} )  
> or maybe  
> @projects = Project.find(:all, :conditions =\> [‘demo\_domain \<\> ?’, nil])
> 
> But, I think you get the idea.

@projects = Project.find(:all, :conditions =\> [‘demo\_domain \<\> ?’, ‘’])  
is the correct syntax and works fine.

@projects = Project.find(:all, :conditions =\> [‘demo\_domain \<\> ?’, nil])  
returns nothing 😕

It works initially when demo\_domain is populated through a form.  
However, the only way to remove the demo\_domain is to empty the field  
via the same form. This is a problem because emptying the field does not  
set the value of demo\_domain to ‘’.

Help?

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 18, 2009, 3:20pm UTC](https://www.ruby-forum.com/t/conditions-issue/180360/4 "2009-12-18T15:20:46Z")

</div>

Pale H. wrote:

> > I really need to ask : why the for loop instead of ‘each’ ?
> 
> Why not? It works perfectly.

Because it’s Ruby? 😉

> @projects = Project.find(:all, :conditions =\> [‘demo\_domain \<\> ?’, nil])  
> returns nothing 😕
> 
> It works initially when demo\_domain is populated through a form.  
> However, the only way to remove the demo\_domain is to empty the field  
> via the same form. This is a problem because emptying the field does not  
> set the value of demo\_domain to ‘’.
> 
> Help?

[http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002210&name=validate\_on\_update](http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002210&name=validate_on_update)

In your model, do something like this:

# I am the king of naming methods.

validate\_on\_update :set\_demo\_domain\_to\_nil\_if\_empty

def set\_demo\_domain\_to\_nil\_if\_empty  
demo\_domain = nil if demo\_domain.empty?  
end

See what you get. Mind you, this is untested, I’ve never used this  
before… I just found it in the docs and thought it might be what you  
wanted.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 18, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/conditions-issue/180360/5 "2009-12-18T15:44:32Z")

</div>

Pale H. wrote:

> > I really need to ask : why the for loop instead of ‘each’ ?
> 
> Why not? It works perfectly.
> 
> > Also …  
> > @projects = Project.find(:all, :conditions =\> {:demo\_domain =\> !nil} )  
> > or maybe  
> > @projects = Project.find(:all, :conditions =\> [‘demo\_domain \<\> ?’, nil])
> > 
> > But, I think you get the idea.
> 
> @projects = Project.find(:all, :conditions =\> [‘demo\_domain \<\> ?’, ‘’])  
> is the correct syntax and works fine.
> 
> @projects = Project.find(:all, :conditions =\> [‘demo\_domain \<\> ?’, nil])  
> returns nothing 😕

Welcome to the wonderful world of SQL three-valued logic! You can’t use  
\<\> (or != , which also works in SQL) to test for NULL. You want  
:conditions =\> ‘demo\_domain IS NOT NULL’

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalidÂ

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 18, 2009, 4:33pm UTC](https://www.ruby-forum.com/t/conditions-issue/180360/6 "2009-12-18T16:33:58Z")

</div>

Pale H. wrote:

> > In your model, do something like this:
> > 
> > # I am the king of naming methods.
> > 
> > validate\_on\_update :set\_demo\_domain\_to\_nil\_if\_empty
> > 
> > def set\_demo\_domain\_to\_nil\_if\_empty  
> > demo\_domain = nil if demo\_domain.empty?  
> > end

> I managed to solve it another way. See below:
> 
> @projects = Project.find(:all, :conditions =\> [‘demo\_domain \<\> ?’, nil  
> || ‘’])
> 
> Thanks for your help, Aldric

Marnen is right - I wasn’t thinking in an SQL context.  
I recommend against your current solution. Nothing is more important in  
a database than integrity, and you really don’t want to have two values  
if you really only mean to have one. It will bite you someday.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 18, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/conditions-issue/180360/7 "2009-12-18T15:25:35Z")

</div>

Aldric G. wrote:\>

> [http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002210&name=validate\_on\_update](http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002210&name=validate_on_update)
> 
> In your model, do something like this:
> 
> # I am the king of naming methods.
> 
> validate\_on\_update :set\_demo\_domain\_to\_nil\_if\_empty
> 
> def set\_demo\_domain\_to\_nil\_if\_empty  
> demo\_domain = nil if demo\_domain.empty?  
> end
> 
> See what you get. Mind you, this is untested, I’ve never used this  
> before… I just found it in the docs and thought it might be what you  
> wanted.

I managed to solve it another way. See below:

@projects = Project.find(:all, :conditions =\> [‘demo\_domain \<\> ?’, nil  
|| ‘’])

Thanks for your help, Aldric
