Skip to content

Instantly share code, notes, and snippets.

@hectorperez
Last active August 29, 2015 14:02
Show Gist options
  • Save hectorperez/c8c8698dfdeaf52f4b35 to your computer and use it in GitHub Desktop.
Save hectorperez/c8c8698dfdeaf52f4b35 to your computer and use it in GitHub Desktop.
Callback in a module in Ruby on Rails
# http://stackoverflow.com/questions/7444522/is-it-possible-to-define-a-before-save-callback-in-a-module
class Model
include MyModule
#...
end
# lib/my_module.rb
module MyModule
extend ActiveSupport::Concern
included do
before_save :do_something
end
def do_something
#do whatever
end
end
# config/application.rb
module Ilitia
class Application < Rails::Application
config.autoload_paths += Dir["#{config.root}/lib"]
#...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment