Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does self do in ruby on rails?

I'm on railcasts just practicing some rails and have come across something I'm trying to understand.

I didn't get what the "self" on the authenticate method was doing. So I deleted it and tested the login of my app to see if it would show an error and it did:

error:

**NoMethodError in SessionsController#create
undefined method `authenticate' for #<Class:0x00000102cb9000**>

I would really appreciate if someone could explain exactly what that "Self" is doing. I was trying to figure out exactly what was going on but can't get my head around it.

Method is defined in model and called in sessions_controller.. I've been continuously deleting my app and starting from scratch to get the hang of it and many things make sense to me each time i start again but I'm stuck at "self".

I'm just the type of person who likes to understand why something works.

controller:

 def create
    user = User.authenticate(params[:email], params[:password])
    if user
      session[:user_id] = user.id
      redirect_to root_path, :notice => "Logged In"
    else
      flash.now.alert = "Invalid credentials"
      render "new"
    end
  end

model:

 def self.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
like image 246
LondonGuy Avatar asked Sep 22 '11 19:09

LondonGuy


1 Answers

This is a basic ruby question. In this case, self is used to define a class method.

    class MyClass
      def instance_method
        puts "instance method"
      end

      def self.class_method
        puts "class method"
      end
    end

Which are used like this:

    instance = MyClass.new
    instance.instance_method

Or:

    MyClass.class_method

Hope that clears things up a little bit. Also refer to: http://railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/

like image 123
rdvdijk Avatar answered Oct 08 '22 06:10

rdvdijk