Conditions Issue

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.

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.

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 :confused:

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?

Pale H. wrote:

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

Why not? It works perfectly.

Because it’s Ruby? :wink:

@projects = Project.find(:all, :conditions => [‘demo_domain <> ?’, nil])
returns nothing :confused:

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

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.

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 :confused:

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
[email protected]Â

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.

Aldric G. wrote:>

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