Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling base class method from overloaded method in sub-class

Perhaps I am just not using the correct terminology for Ruby (and if I am please correct me), but Google just isn't helping me on this one.

What I have is a class (call it OrderController) that extends another class (call it BaseStoreController). In the BaseStoreController I have defined a before_filter that is used throughout my site, with the small except of my OrderController. In this very particular situation I needed to define a custom before_filter that needs to do some additional logic and then call the before_filter defined in my BaseStoreController.

What I do not know is how to do this.

Here is what I've tried, but it appears that the 'super' keyword isn't what I was expecting it to be:

class BaseStoreController < ActionController::Base     before_filter :authorize      protected         def authorize             #common authroization logic here         end  end 

and

class OrderController < BaseStoreController     before_filter :authorize      protected         def authorize             #additional authroization logic here             super.authorize         end  end 

The end result of my code is that the authorize method in the OrderController is failing with the following error:

 You have a nil object when you didn't expect it! The error occurred while evaluating nil.authorize 
like image 374
Jason Whitehorn Avatar asked Oct 10 '09 04:10

Jason Whitehorn


People also ask

Can method overloading be done in subclass?

Usually, method overloading happens inside a single class, but a method can also be treated as overloaded in the subclass of that class — because the subclass inherits one version of the method from the parent class and then can have another overloaded version in its class definition.

How do you call a base class method from a derived class?

Using a qualified-id to call a base class' function works irrespectively of what happens to that function in the derived class - it can be hidden, it can be overridden, it can be made private (by using a using-declaration), you're directly accessing the base class' function when using a qualified-id.

Is method overloading possible in two different classes?

Yes it is overloading , This overloading is happening in case of the class ' C ' which is extending P and hence having two methods with the same nam e but different parameters leading to overloading of method hello() in Class C .


1 Answers

Have you tried calling the base class's "authorize" method with just "super" instead of "super.authorize"?

like image 106
pythonquick Avatar answered Sep 18 '22 09:09

pythonquick