Forum: Ruby on Rails How to test for a Null or empty object?

7759594c66801d6f311ff38fd3ddef2a?d=identicon&s=25 Chris Gunnels (ballhogjoni)
on 2009-03-10 06:02
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
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-03-10 10:03
(Received via mailing list)
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 Gunnels <rails-mailing-list@andreas-s.net>
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-10 12:55
(Received via mailing list)
Chris Gunnels 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!
7759594c66801d6f311ff38fd3ddef2a?d=identicon&s=25 Chris Gunnels (ballhogjoni)
on 2009-03-10 13:57
Colin Law 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 Gunnels <rails-mailing-list@andreas-s.net>

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.
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-10 15:14
(Received via mailing list)
>> 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.