Topic: Call method with another method in the same class

class ReviewsController < ApplicationController
     
     def method1
     
     end

     def method2
     
     // how do I call method1 from within method2 ?
     
     end
     
end

Re: Call method with another method in the same class

You can just call it like this:

class ReviewsController < ApplicationController
  def method1
  end

  def method2
    method1
  end
end


However, this is inside a controller so it's not very common to call one action inside of another (that is if method1 is an action). I usually extract the common code into a third method and make it protected so it's not an action. Hope that makes sense.

Last edited by ryanb (2006-09-04 20:18:53)

Railscasts - Free Ruby on Rails Screencasts

Re: Call method with another method in the same class

Thanks again man. Makes sense.