Re: Noob question .each problem

radu puspana
2010-01-04T16:10:15Z

On Jan 3, 6:50 pm, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> On Jan 3, 4:37 pm, radu puspana
<radupusp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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)}

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,