Call a class method in an after_save
Make it an instance method by removing self
.
# now an instance method
def update_menu
@@menu = Category.all
end
It doesn't make much sense to have an after_save
callback on a class method. Classes aren't saved, instances are. For example:
# I'm assuming the code you typed in has typos since
# it should inherit from ActiveRecord::Base
class Category < ActiveRecord::Base
attr_accessible :name
end
category_one = Category.new(:name => 'category one')
category_one.save # saving an instance
Category.save # this wont work
after_save :update_menu
def updated_menu
self.class.update_menu
end
this will call the class update_menu method
after_save 'self.class.update_menu'
Rails will evaluate a symbol as an instance method. In order to call a class method, you have to pass a string which will be evaluated in the correct context.
NOTE: This only works with rails 4. See Erez answer for Rails 5.