Skip to content

Instantly share code, notes, and snippets.

@eljojo
Created September 23, 2014 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eljojo/6bd2a4d21a150c0b21d3 to your computer and use it in GitHub Desktop.
Save eljojo/6bd2a4d21a150c0b21d3 to your computer and use it in GitHub Desktop.
example of what a callback is using active record
class User < ActiveRecord::Base
# here we set tha callback to be executed before saving the user
before_save :create_username
def create_username
if username.blank?
self.username = "example username"
end
end
end
user = User.new # first we instantiate the new user
# then, we save the user
# this is the moment when the callback gets executed
user.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment