Hello everyone, I have a rails app which uses email as login name. At some point, I have to search the user, in the User model by email. For some reason, the method User.find_by_email(email) returns nil, even though the user is in the database. The same applies for any find_by_* method that searches by any database column. I was able to find the user using the User.find Does anyone have an idea of what might be going on? thanks, -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 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.
On Thu, Sep 22, 2011 at 7:21 PM, Fred <frederiko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello everyone, > > I have a rails app which uses email as login name. At some point, I > have to search the user, in the User model by email. For some reason, > the method User.find_by_email(email) returns nil, even though the user > is in the database. > > The same applies for any find_by_* method that searches by any > database column. I was able to find the user using the User.find > > Does anyone have an idea of what might be going on? >Is this ruby 1.9? Maybe ... this is related to string encoding of the columns ? Check the string encoding of the string by which you are searching and the one that is used in the database. Please show exact code samples for how you used User.find (with succesful result) and User.find_by_email (without result). For this scenario of encoding issues, please show the email.encoding on the result from from the find => User.first.email.encoding HTH, Peter -- 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.
Hi Fred, I was having the same issue. I finally resolved it by defining the find_by_email method in my app/models/user.rb file... Example: ----------- def self.find_by_email(email) where(:email => email).first end ----------- That did it for me! Good luck, Aaron Gray -- Posted via http://www.ruby-forum.com/. -- 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.