When I omitted ‘self.’ from the method definition, Rails couldn’t find a method.
undefined method `authenticate' for #<Class:0x00000103b8c640>
...
app/controllers/sessions_controller.rb:6:in `create'
Here is the snippet from the source,
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
and authenticate() is called from SessionController
class SessionConstroller < ApplicationController
def create
user = User.authenticate(params[:email], params[:password])
...
end
When I add ‘self.’ to the definition of authenticate(), it works without error.
What’s the difference between adding ‘self.’ and nothing to the method definition in Rails 3.x?
self.authenticate(...)
authenticate(...)
Adding
selfto the method definition makes it a class method rather than an instance method. So when you try and callUser.authenticate, it only works if you defined the method on the class rather than for instances ofUser.Like this:
Try playing around with small example codes like this, rather than straight away within the larger context.