Search code examples
ruby-on-railsmethodsoverriding

Overide method in gem


I am using AwesomeNestedSet and have a place in my app that calls the parent method on a model instance. In some cases, parent returns nil. That's fine but in the cases where it does I need it to return a string of root instead.

I have this scenario in 3 places across my app so want a centralised place I can write this. In my Category model I did try:

def parent
  parent || 'root'
end

But of course that just gets everything into a loop.

How can I get this functionality but without causing it to loop?

Neil


Solution

  • This should do the trick:

    def parent
      super || 'root'
    end