ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.2meta-search

Find which alias method is called in rails model


I am new to rails development. I have created some aliases to a method and I want to know that which alias is called.

I have this code.

alias_method :net_stock_quantity_equals :net_stock_quantity
alias_method :net_stock_quantity_gte :net_stock_quantity
alias_method :net_stock_quantity_lte :net_stock_quantity
alias_method :net_stock_quantity_gt :net_stock_quantity
alias_method :net_stock_quantity_lt :net_stock_quantity

def net_stock_quantity
  #some code here
end

I want to know that user has called which alias. Like if user calls net_stock_quantity_equals then I should know that the user has called net_stock_quantity_equals not net_stock_quantity.

Any help would be appreciated.


Solution

  • def net_stock_quantity(alias_used = :net_stock_quantity)
        method_called = caller[0]
        #some code
    end
    

    The method_called will contain the name of called alias.