Matthew Wallace
2010-Nov-28 22:27 UTC
Dynamic find_by method returning nil in a class method
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("my-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org",
"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("my-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org") 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Try using user = self.find_by_email(email) On Nov 29, 7:27 am, Matthew Wallace <mswallace....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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...-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org", "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...-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org") 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Try this one user = self.find_by_email(email) On Nov 29, 7:27 am, Matthew Wallace <mswallace....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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...-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org", "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...-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org") 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.