Когда я опустил «я».из определения метода Rails не смог найти метод.
undefined method `authenticate' for #<Class:0x00000103b8c640>
...
app/controllers/sessions_controller.rb:6:in `create'
Вот фрагмент из источника,
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation
attr_accessor :password
before_save :encrypt_password
...
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
def authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
и authenticate () вызывается из SessionController
class SessionConstroller < ApplicationController
def create
user = User.authenticate(params[:email], params[:password])
...
end
Когда я добавляю «я».к определению authenticate (), он работает без ошибок.
Какая разница между добавлением «self».и ничего к определению метода в Rails 3.x?
self.authenticate(...)
authenticate(...)