Created
September 23, 2014 16:58
-
-
Save eljojo/6bd2a4d21a150c0b21d3 to your computer and use it in GitHub Desktop.
example of what a callback is using active record
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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