# Class versus instance method - how?

**URL:** <https://www.ruby-forum.com/t/class-versus-instance-method-how/164600>\
**Category:** Rails\
**Created:** [April 2, 2009, 11:33pm UTC](https://www.ruby-forum.com/t/class-versus-instance-method-how/164600 "2009-04-02T23:33:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [April 2, 2009, 11:33pm UTC](https://www.ruby-forum.com/t/class-versus-instance-method-how/164600/1 "2009-04-02T23:33:16Z")

</div>

I have a file call authentication.rb in rails root/lib directory which  
has the following method defined amongst others:

module Authentication  
…  
def authorize\_admin?  
logged\_in? && has\_role?(‘admin’)  
end  
…  
end

This module is included into application.rb as follows:

class ApplicationController \< ActionController::Base

# Ryan’s stripped down Authentication module from

Restful\_Authentication  
include Authentication

# Tim H.'s RoleRequirement module from his plugin

include RoleRequirementSystem

end

I am also using Ryan Heath’s excellent navigation\_helper plugin which  
has a “current\_tab” class method. You set the current\_tab at the  
controller level as follows:

class UsersController \< ApplicationController

before\_filter :login\_required  
current\_tab(self.authorize\_admin? ? :clients : :basic\_data)  
…  
end

I was trying the conditional form for setting the current tab based on  
whether the logged\_in? user was an “admin” or a plain “user”. That did  
not work. I got the following exception:

NoMethodError in UsersController#edit

undefined method `authorize\_admin?’ for UsersController:Class in line 4  
which is the current\_tab method above. I do not fully understand why.  
The current\_tab is a class method why can’t it take an argument  
containing an instance method?

I got around the above issue by doing this:

class UsersController \< ApplicationController

before\_filter :login\_required  
current\_tab :clients

def edit  
self.class.current\_tab :basic\_data unless authorize\_admin?  
end  
…  
end

So at the method level, I override the current\_tab based on desired  
condition using the authorize\_admin? method. This works.

So my question in the above context is: how do you call an instance  
method as an argument to the class method?

Bharat  
…  
end

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [April 2, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/class-versus-instance-method-how/164600/2 "2009-04-02T23:40:33Z")

</div>

On Apr 2, 10:33 pm, Bharat R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> end

If you don’t understand why this doesn’t work, then you probably don’t  
understand when this code runs. This code runs when the class is  
loaded from users\_controller.rb. In particular self is the class  
UsersController, so you can’t call authorize\_admin? on it, since that  
is an instance method and you don’t have an instance of  
UsersController.

In production this code only executes once (when the app starts), ie  
there is no request or current user - clearly you can’t be doing user  
specific things at this point.

Fred

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [April 2, 2009, 11:50pm UTC](https://www.ruby-forum.com/t/class-versus-instance-method-how/164600/3 "2009-04-02T23:50:38Z")

</div>

Thank you Fred. You are right, I did not understand it. Now I do. I  
do get a bit “mixed-up” with all these “mix-ins” sometimes 🙂
