Ruby on Rails Monday, November 29, 2010

Try using user = self.find_by_email(email)

On Nov 29, 7:27 am, Matthew Wallace <mswallace....@gmail.com> wrote:
> I have a user Model with a class method that I am using to do some
> authentication
>
> basically something like this
>
> class User < ActiveRecord::Base
>
>   attr_accessor   :password
>   attr_accessible :first_name, :last_name,
>                   :email, :birth_date, :sex,
>                   :password, :password_confirmation
>
>   email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
>
>   validates :first_name,  :presence     => true,
>                           :length       => { :maximum => 50 }
>
>   validates :last_name,   :presence     => true,
>                           :length       => {:maximum => 50 }
>
>   validates :email,       :presence     => true,
>                           :format       => { :with => email_regex },
>                           :uniqueness   => { :case_sensitive =>
> false }
>
>   validates :password,    :presence     => true,
>                           :length       => {:within => 6..20},
>                           :confirmation => true
>
>   validates :sex,         :presence     => true
>
>   validates :birth_date,   :presence    => true
>
>   def self.authenticate(email, submitted_password)
>     user = find_by_email(email)
>     puts user
>     return nil if user.nil?
>     return user if user.has_password?(submitted_password)
>   end
>
> end
>
> as you can see I left out some code but the problem I am having is
> where you can see that I have a "puts user". when running this method
> from the Console like so
>
> User.authenticate("m...@email.com", "foobar") the local user object in
> the authenticate method returns nil from the puts
>
> now in the console if I do this
>
> User.find_by_email("m...@email.com") the consol returns me a valid user
> object from the database
>
> what am I missing here ?
>
> thanks in advance for any insight you can offer.

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment