After completing two projects for my engineering boot camp portfolio, I decided to write a quick post about creating and using callback functions in Ruby on Rails::ActiveRecord. Please click the link provided for the documentation and full guide on ActiveRecord::Callbacks. In this article, I will define “What is ActiveRecord?”, “What are callback functions?”, and “What are ActiveRecord::Callbacks?”
What is Active Record?
When using Ruby on Rails in your application, whether for the frontend, backend or both, Active Record is your link between your models and your database. So, instead of having to query your database like this:
you can define methods (or actions) that will do the query for you like this:
which will yield the same results as manually querying the database. This is possible due to the Active Records ORM (Object Relational Mapping) technique that easily stores and retrieves data without the hassle of writing a bunch of SQL statements. So now that we know that, let's discuss callback functions, and how does Active Record use them?
What are callback functions?
According to MDN, “A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.” -MDN web_docs
In my terms, callback functions are functions that are used before or after another function or action has been performed. For instance:
when you wash your laundry, there are the major functions of completing this task:
- gather clothes
- sort clothes
- add clothes to the washer
- add detergent and softener
- once complete, gather clothes
- dry clothes
- once complete, laundry is finished!
Now let's say we wanted to sort clothes again after washing to separate specific items from the entire load. We can simply “call back” the sort function(step 2) before we dry clothes (step 6) and do not need to write a new function to handle what we have already defined in a previous step. Call back functions are also used recursively, that is when you need the action to repeat continuously until the stopping point has been reached. Not to go too far into recursion, but to give an idea of how this would work using the above example:
set steps 1–6 in a new function called doingLaundry() and have it complete all steps until every basket of laundry is complete, therefore doingLaundry() would use callbacks repeatedly until all baskets were finished, and thus running step 7, the stop condition, and finishing the laundry.
What are ActiveRecord callbacks?
Active Record uses callbacks but in a different syntax compared to other program languages. In ActiveRecord, I can set global functions, which are called methods or actions) in the application controller (the main controller that all others will inherit from in the application) and pass the actions around, accordingly. Here is an example of where I defined a method(:authorize) in the application controller:
I then passed that method to the separate controllers that may need the “:authorize” method, inherited from the application controller:
The before_action tells the controller when to use the methods listed. The before, after, skip, and around are a few common verbs and phrases for using the callback methods(functions). Please refer to the ActiveRecord guides for a complete list of these method verbs and how to best use them.
Also, the example snapshot above details a couple of callback methods in the controller itself (def newIce & def newUser) and does not need to be defined in the application controller for all controllers to inherit. Still, a method verb or phrase to set it globally in the controller will be needed.