How to test for a Null or empty object?


#1

I have the following action that is supposed to test whether a username
exists or not. It seems that the first condition never fails. I have put
nothing in the params[:l] hash and the first condition executes. Any
ideas?

def index
@oLogin = User.find_by_login(params[:l])
if @oLogin.login != nil
@sLoggedInName = @oLogin.login + “, you’ve logged in successfully”
else
render :controller => ‘session’
end
end


#2

I think it is @oLogin that you need to test for nil before accessing
@oLogin.login. This is to check whether the find returned anything.

2009/3/10 Chris G. removed_email_address@domain.invalid


#3

Chris G. wrote:

I have the following action that is supposed to test whether a username
exists or not. It seems that the first condition never fails. I have put
nothing in the params[:l] hash and the first condition executes. Any
ideas?

def index
@oLogin = User.find_by_login(params[:l])
if @oLogin.login != nil

if @oLogin

You ought to read some Rails tutorials and projects for a while; these
patterns
must soak in!

Also, nobody around here uses HN like oLogin. We know it’s an 'o’bject
already!


#4

Colin L. wrote:

I think it is @oLogin that you need to test for nil before accessing
@oLogin.login. This is to check whether the find returned anything.

2009/3/10 Chris G. removed_email_address@domain.invalid

Thx…

Also, nobody around here uses HN like oLogin. We know it’s an 'o’bject
already!

good point…I come from a PHP background so not everything is an
object, I just have to get used to that.


#5

Also, nobody around here uses HN like oLogin. We know it’s an 'o’bject
already!

good point…I come from a PHP background so not everything is an
object, I just have to get used to that.

Ruby offers very flexible syntax, with many different alternatives for
each
statement, so we generally try to select the sequence that…

 ...most closely resembles English...

…or whatever your favorite human language is.