私は、認証メソッドを持つユーザーモデルを持っています。rails dynamic find_byメソッドがクラスメソッド内で動作しない
モデルを使ってレールコンソールでテストすると、ユーザーをうまく作成できて、電子メールで検索して、このように完全にユーザーを返すことができます。今、私はこのような認証方法を呼び出し、
user = User.authenticate("[email protected]", "foobar")
nilを私の認証方法が戻るにはユーザーにモデルを置くUSER文を返すようにしようとした場合
user = User.find_by_email("[email protected]")
この
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :first_name, :last_name,
:email, :birth_date, :sex,
:password, :password_confirmation
def self.authenticate(email, submitted_password)
user = find_by_email(email)
puts user #this returns nil so my class is never able to authenticate the user
return nil if user.nil?
return user if user.has_password?(submitted_password)
end
end
ようになります
私は何が問題なのか迷っています。この問題に目を向ける人もいるでしょう。
これは本当の解決策でした。私はあなたの提案を見る前にそれを見つけました。いいですよ。 – mattwallace
@mswallaceおそらくこの答えを受け入れる? –