Nil in ActiveRecord and SQL

Hi all !
How I can do like this, because it dosen’t work ?
SomethingClass.find(:all, :conditions => [“something_attribute =
?”,nil])

On 1/25/07, Walde M. [email protected] wrote:

How I can do like this, because it dosen’t work ?
SomethingClass.find(:all, :conditions => [“something_attribute =
?”,nil])

In SQL, you need “something IS NULL” rather than “something = NULL”.
The above code should have raised a StatementInvalid exception saying
as much.

Pass conditions as a hash to have Active Record choose whether to use
IS NULL for you:
SomethingClass.find(:all, :conditions => { :something_attribute => nil
})

Or use the find_all_by_* shorthand:
SomethingClass.find_all_by_something_attribute(nil)

jeremy

Jeremy K. wrote:

On 1/25/07, Walde M. [email protected] wrote:

How I can do like this, because it dosen’t work ?
SomethingClass.find(:all, :conditions => [“something_attribute =
?”,nil])

In SQL, you need “something IS NULL” rather than “something = NULL”.
The above code should have raised a StatementInvalid exception saying
as much.

Pass conditions as a hash to have Active Record choose whether to use
IS NULL for you:
SomethingClass.find(:all, :conditions => { :something_attribute => nil
})

Or use the find_all_by_* shorthand:
SomethingClass.find_all_by_something_attribute(nil)

jeremy

Thank for help Jeremy !
SomethingClass.find(:all, :conditions => { :something_attribute => nil

}) It work excellently !!!

But of course, problem is bigger.
When I try do something like that :

SomethingClass.find(:all, :conditions => ({ :something_attribute1 => nil

} or { :something_attribute1 => Time.now.mday } ) and ({ :something_attribute2 => nil
} or { :something_attribute2 => Time.now.hour } ) and ({ :something_attribute3 => nil
} or { :something_attribute3 => Time.now.min } ))
it doesn’t work again.
Could you explain me how I should use “or” and “and” .

})

Jeremy K. wrote:

On 1/25/07, Walde M. [email protected] wrote:

Check out the ez_where plugin if you’d like to easily translate Ruby’s
operators to SQL.

jeremy

One more ,thank for help Jeremy !
On another way I apply “IS NULL” in “text form of conditions” instead
of “= nil”.
And with “or” and “and”, is great !
… a thousand thanks !

On 1/25/07, Walde M. [email protected] wrote:

Thank for help Jeremy !
it doesn’t work again.
Could you explain me how I should use “or” and “and” .

Check out the ez_where plugin if you’d like to easily translate Ruby’s
operators to SQL.

jeremy