From the course: Ruby on Rails 6 Essential Training: Models and Associations

Use callbacks to automate actions - Ruby on Rails Tutorial

From the course: Ruby on Rails 6 Essential Training: Models and Associations

Start my 1-month free trial

Use callbacks to automate actions

- Now that we've seen an overview of callbacks and we understand what they are and why we need them, let's take a look at a specific example and then try writing our own. This code shows three callbacks inside the customer class, The first is a before validation call back and it's formats the phone number that presumably this customer record has. That's the kind of thing you would want to perform before the validation. Format the phone number, get in the right order and then validate it to see if it's valid. I find that things that modify the values are typically done before the validation. Next, I have a before save callback called geocode address, the idea here is that after the record has gone through its validations, but before we save it, we want to go out to a third party service and find out that latitude and longitude for the customer's address. And we'll store that geocode information in the record as we save it, so we want to get those values before we save it, but we don't want to do it until we know the records is valid, here's no point in geocoding it, until we've gone through that validation. And then last of all I have an after commit callback called notify admins, the idea here is that anytime we make a change this record, after the record has been fully saved to the database, because it's a customer record, we want to let admins know that there was a change. It might send an email or post something to a log file, you get the idea. Each one of those callbacks is calling a method that's in the class, you can see, I have placeholders for those as private methods that are below. And that's typically the way it works, you register the call back at the top, that's where you have before validation, before save and after commit, that's registering it, and then you have to actually define the method down below. Let's try an example in our application, let's go into our subject class and let's write a couple of callbacks here. I'm just going to drop down here and let's write the first one is going to say, after we create a record, let's log that creation, and I'm just going to drop down here and I'll make a private method, nothing really needs to call it outside of this class, it's only going to be called from inside the subject class, so I'll make it private, and I'm going to call it "def log create" and inside there let's log something. We learned how to log in the controllers and views course, we can use that here, let's just say subject ID, and then I'll drop in the ID. I have access to all the attributes inside this object was created. Okay so now, in addition to this one, let's copy it, and I'm going to paste it two more times. I'm going to make another version of it here that is after update, and then I just want us to see the difference between these and let's do an after save. Okay, I'm going to need two more versions of this method as well, so I'll paste those here and this one will be update and it's going to say the record was updated and this one is going to say save, and this will be the record was saved. So now I have three different callbacks and all they're going to do is log something to the log file, but you could do all sorts of actions here. I also find that I use the after create, after update and after save callbacks, a bit more than I use the other ones. Once we have our callbacks written, let's save it, and we could go over and try it out in the console, but let's start our rails server for a change, we've been doing a lot in the console, let's actually fire up the server, let's open up our web browser, and once it says it's listening, we'll go to local host, every our local host is calling 3000. It pops up with a little JavaScript, this is something we wrote in the controllers and views course. I'm going to click on all subjects, I get a web form here, right? This is a simulation of a login form that we created and you can type in any username and password and it will accept it. There's no actual login going on there. And then I have manage subjects, here I am in this subject area, let's try adding a new subject, okay? New subject, what is its name going to be? I'm going to say, this is a callback subject, the position, I'll make it four, and I'll say that it should be visible. Now, remember we do have validations in place, so we do have to fill out these things or else it'll fail our validations. Create subject, subject was created, we see it right here, did our callbacks take place? If we look over here in our console, you can actually see them right here, subject idea was created and saved. It has two different callbacks, now it's not doing two actions It's just calling two different callbacks, the first one is putting one bit of text and the second one is putting another bit of text. I wanted you to see that both of them are triggered. Notice that the updated method did not get called. Let's try going back in and let's edit that subject, call back subject and I'll change its name to call back example. Visible, yes. Update, subject updated. Now if we look back over here, let's scroll down and you'll see the subject was updated and subject was saved. So it called two call backs again, but it didn't call created this time, it called updated, but save got called in both cases, it applies to both of them, so anything you put there would run, whether you're creating or whether you're updating. Now, I'm just looking at those in the console, if you were actually to go to your log file, look inside there you'd see the exact same thing, scroll down, and, here it is subject six was created and saved. So that's all there is to writing callbacks, you decide where you want to place the call back, you register a method for it and then you define the method to do whatever you want.

Contents