# Noob question .each problem

**URL:** <https://www.ruby-forum.com/t/noob-question-each-problem/181004>\
**Category:** Rails\
**Created:** [January 3, 2010, 5:38pm UTC](https://www.ruby-forum.com/t/noob-question-each-problem/181004 "2010-01-03T17:38:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![radu\_puspana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@radu\_puspana](https://www.ruby-forum.com/u/radu_puspana)\
**Post date:** [January 3, 2010, 5:38pm UTC](https://www.ruby-forum.com/t/noob-question-each-problem/181004/1 "2010-01-03T17:38:28Z")

</div>

First, A Happy New Year to you all  
Second :

this is my login action form the admin controller:  
def login  
if request.post?  
user = User.authenticate(params[:name], params[:password])  
if user  
if user.banned\_status == “true”  
flash.now[:notice] = “We are sorry.You can not log in beause  
you have been banned.Contact the admin at removed\_email\_address@domain.invalid to solve this  
problem”

```
    elsif user.blocked_status == "true"
      flash.now[:notice] = "We are sorry.You are still

```

# blocked.Contact the admin at removed\_email\_address@domain.invalid to solve this problem" else session[:user\_id] = user.id redirect\_to :action =\> “index” end else flash.now[:notice] = “Invalid user/password combination # {user.id}” end# 2nd if end# 1st if end#login

this is my user model for the authenticate method :  
def self.authenticate(name, password)  
user = User.find(:all, :conditions =\> [“name = :param”,{:param  
=\> name}])  
if user.size \> 0  
user.each do |correct\_user|  
expected\_password = User.encrypted\_password(password,  
correct\_user.salt)  
if correct\_user.hashed\_password == expected\_password  
idx = user.index(correct\_user)

```
               return user[idx]
            end
        end#do
        user = nil
    else
      user = nil
    end#1st if
end#def

```

======  
and the login view :

 \<% form\_tag do %\> 
 Name: \<%= text\_field\_tag :name, params[:name] %\> 

```
        <div>
            <label for="password">Password:</label>
            <%= password_field_tag :password, params[:password] %>
        </div>

        <div>
            <%= submit_tag "Login", :name => nil %>
        <div>
    </fieldset>
<% end %>

```

So the thing is that in my db i have two users with the same name, but  
no twith the same password.  
id = 2  
name = radu  
surname = puspana  
hashed\_password = 6b2711b337ae9b5c8a5932e60955f25dc42bcec1

```
         id = 4
       name = radu
    surname = gica

```

hashed\_password = caf5488d972483c5b064a1afbe30b4370391ab73

The problem is that when i try to login with the first name, radu and  
the password admin, it works fine, but if i try to login in with the  
second name(id =4) radu andthe pass gica1, it return the nil value  
back to the controller, so user in the controller will be nil, showing  
the message “Invalid username/password” although the name and pass are  
correct…

what the heck???

a billion thx in advance,  
radu

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![radu\_puspana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@radu\_puspana](https://www.ruby-forum.com/u/radu_puspana)\
**Post date:** [January 3, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/noob-question-each-problem/181004/2 "2010-01-03T17:51:56Z")

</div>

On Jan 3, 4:37 pm, radu puspana [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> user.each do |correct\_user|  
> expected\_password = User.encrypted\_password(password, correct\_user.salt)  
> if correct\_user.hashed\_password == expected\_password  
> idx = user.index(correct\_user)  
> return user[idx]  
> end  
> end

This is somewhat unwieldy - why the busines with index when you could  
just return correct\_user ? Even better, use a method like detect - the  
code above is equivalent to  
user.detect {|current\_user| current\_user == User.encrypt(password,  
current\_user)}  
You’ll also find your code reads more easily if variables that contain  
collections are pluralised (ie users = User.find :all rather than user  
= User.find( :all))

This shouldn’t change the result of the code however. Seems to me that  
the data in your table might just be bad (or you’re typing in the  
wrong password) - What is the value of User.encrypted\_password  
( ‘gica1’, User.find(4).salt) ?

Having users with the same name is really rather weird - every website  
I can think of requires usernames to be unique

Fred

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![radu\_puspana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@radu\_puspana](https://www.ruby-forum.com/u/radu_puspana)\
**Post date:** [January 3, 2010, 5:49pm UTC](https://www.ruby-forum.com/t/noob-question-each-problem/181004/3 "2010-01-03T17:49:36Z")

</div>

problem solved!!! thx to me 🙂

On Jan 3, 6:37 pm, radu puspana [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> you have been banned.Contact the admin at removed\_email\_address@domain.invalid to solve this  
> flash.now[:notice] = "Invalid user/password combination #  
> user.each do |correct\_user|  
> user = nil  
>   
> \<% end %\>  
> name = radu  
> what the heck???
> 
> a billion thx in advance,  
> radu

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![radu\_puspana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@radu\_puspana](https://www.ruby-forum.com/u/radu_puspana)\
**Post date:** [January 4, 2010, 5:24pm UTC](https://www.ruby-forum.com/t/noob-question-each-problem/181004/4 "2010-01-04T17:24:22Z")

</div>

On Jan 4, 4:10 pm, radu puspana [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and this line : user.detect {|current\_user| current\_user ==  
> User.encrypt(password,current\_user)}, shouldn’t be something like  
> user.detect {|current\_user| current\_user == User.encrypt  
> (password,current\_user.salt)}

that’s right.  
This means: iterate over the user collection, passing each element  
into the block as current\_user. return the first element for which the  
condition in the block returns true.

> > You’ll also find your code reads more easily if variables that contain  
> > collections are pluralised (ie users = User.find :all rather than user  
> > = User.find( :all))
> 
> thx so much for this tip too, it doesn’t have to do anything with the  
> line user.detect {|current\_user| current\_user == User.encrypt  
> (password, current\_user)} right?

no. just a stylistic thing

Fred

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![radu\_puspana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@radu\_puspana](https://www.ruby-forum.com/u/radu_puspana)\
**Post date:** [January 4, 2010, 5:12pm UTC](https://www.ruby-forum.com/t/noob-question-each-problem/181004/5 "2010-01-04T17:12:04Z")

</div>

On Jan 3, 6:50 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> This is somewhat unwieldy - why the busines with index when you could  
> just return correct\_user ? Even better, use a method like detect - the  
> code above is equivalent to  
> user.detect {|current\_user| current\_user == User.encrypt(password,  
> current\_user)}

the manbo jumbo with idx was that it was the only method which worked.  
as you suggested, i was putting in the wrong password, and the method  
did the correct this, it returned user=nil.  
The name problem, was another bad thing to do, I thought about it and  
it made no sense at all.Mistakenly i forgot that name was in fact the  
username, and i thought that two users can have the same name and  
surname, logically, but not the same username.I will create another  
column with this name,username, and leave the name column alone.  
Thx so much for the alternate version of my code, but i don’t  
understand anything, as i said, i’m a noob, been working with rails  
about 3 months, and studied a nit of Ruby, about 3 chapters, about  
classes, objects and stuff.maybe you could translate it a bit for me,  
if you have the time, i will much appreciate it.

and this line : user.detect {|current\_user| current\_user ==  
User.encrypt(password,current\_user)}, shouldn’t be something like  
user.detect {|current\_user| current\_user == User.encrypt  
(password,current\_user.salt)}

> You’ll also find your code reads more easily if variables that contain  
> collections are pluralised (ie users = User.find :all rather than user  
> = User.find( :all))

thx so much for this tip too, it doesn’t have to do anything with the  
line user.detect {|current\_user| current\_user == User.encrypt  
(password, current\_user)} right?

> This shouldn’t change the result of the code however. Seems to me that  
> the data in your table might just be bad (or you’re typing in the  
> wrong password) - What is the value of User.encrypted\_password  
> ( ‘gica1’, User.find(4).salt) ?
> 
> Having users with the same name is really rather weird - every website  
> I can think of requires usernames to be unique
> 
> Fred

thx so much for all your help,  
have a Happy New Year ok? ,  
sincerely,  
radu

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![radu\_puspana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@radu\_puspana](https://www.ruby-forum.com/u/radu_puspana)\
**Post date:** [January 4, 2010, 5:54pm UTC](https://www.ruby-forum.com/t/noob-question-each-problem/181004/6 "2010-01-04T17:54:48Z")

</div>

On Jan 4, 6:22 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> condition in the block returns true.  
> Sry for bothering you agan but shouldn’t it be :  
> user.detect {|current\_user| current\_user.hashed\_password ==  
> User.encrypt(password,current\_user.salt)} reather than user.detect {|  
> current\_user| current\_user== User.encrypt  
> (password,current\_user.salt)} ??.  
> == means a condition if i recall correctly.  
> because the string that comes out of User.encrypt  
> (password,current\_user.salt)} should be compared with something in the  
> current\_user, namely hashed\_password, not the object refered by the  
> variable current\_user, right?
> 
> > > You’ll also find your code reads more easily if variables that contain  
> > > collections are pluralised (ie users = User.find :all rather than user  
> > > = User.find( :all))
> 
> > thx so much for this tip too, it doesn’t have to do anything with the  
> > line user.detect {|current\_user| current\_user == User.encrypt  
> > (password, current\_user)} right?
> 
> no. just a stylistic thing  
> thx again for the trick:)  
> Fred

much much appreciate it Fred,  
regards,  
radu

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![radu\_puspana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@radu\_puspana](https://www.ruby-forum.com/u/radu_puspana)\
**Post date:** [January 4, 2010, 6:36pm UTC](https://www.ruby-forum.com/t/noob-question-each-problem/181004/7 "2010-01-04T18:36:21Z")

</div>

On Jan 4, 4:53 pm, radu puspana [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (password,current\_user.salt)} should be compared with something in the  
> current\_user, namely hashed\_password, not the object refered by the  
> variable current\_user, right?

correct.

Fred

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![radu\_puspana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@radu\_puspana](https://www.ruby-forum.com/u/radu_puspana)\
**Post date:** [January 4, 2010, 6:56pm UTC](https://www.ruby-forum.com/t/noob-question-each-problem/181004/8 "2010-01-04T18:56:15Z")

</div>

Problem solved!

Hi again Fred,

I tried user.detect {|current\_user| current\_user.hashed\_password  
==User.encrypt(password,current\_user.salt)} and it worked like a  
charm.

thx a million yet again for ALL your help,  
radu

On Jan 4, 7:34 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > == means a condition if i recall correctly.  
> > because the string that comes out of User.encrypt  
> > (password,current\_user.salt)} should be compared with something in the  
> > current\_user, namely hashed\_password, not the object refered by the  
> > variable current\_user, right?
> 
> correct.
> 
> Fred

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
