Find out how ActiveRecord keeps track of the current and previous values of object attributes and how you can determine when attribute values have changed.
- [Instructor] In the last movie, we learned about dirty objects, and how to query an instance of an object to find out if it has changes or not. In this movie, we'll learn how to query individual attributes about their changes. So let's say that we have a product. We create a new product, call it "Couch," and we're going to have a quantity sold of five. So that's going to save it to the database. So we know that that's not going to have changes at the moment. We saw that in the last movie. Now let's make a change to it, let's set the name to be equal to "Sofa" instead of "Couch." So, we already know that we can ask the product whether or not it has changed, but we also can ask the specific attribute, we can ask the name attribute, "Hey, name attribute, have you changed?" I'm not worried about the whole product, I just want to know has the specific attribute changed.
And we do that by using a magic method, that is, it's the attribute name followed by "underscore," "changed," question mark. So we just use ".change" when we want to ask the object but when we want to ask an attribute, we use the attribute name, then underscore, then changed, question mark. It'll come back and tell us if it has changed or not. So in this case, the name has changed, but quantity sold has not changed. Because we didn't make changes to it, it's still five, which is what is in the database. We also have another magic method, that uses the attribute name followed by "underscore" "change," and it'll tell us what the change was.
So "product.name_change" will tell us the change from "Couch" to "Sofa," or, if we want to know just what the old value was, we could just ask for "name," "underscore," "was." "Product.name_was" and it'll tell us, "Couch." "Product.name" would give us the current value, which is "Sofa." We also have the ability to ask, "name change question mark" but to provide a hash of values to ask whether there was a change from something to something else. So for example, I can ask product, "Hey, did your name change from 'Couch' to 'Sofa?' And it returns true. It did.
If I had instead asked it, "Did name change from 'Couch' to 'Loveseat?'", it returns false. So it's still that name change method, but instead of just returning true or false if it changed, we're getting a little more specific and saying, if it changed, check for some specific values. Let's try these in the console, see if you can get a feel for them. So I'm inside the root of my Rails project, and I'm going to launch the rails console, and let's start by creating a brand new product, I already have a couch in my database but I'm going to just create another one.
'Cause we're going to change it, so, "Couch," and, quantity sold, make five. Alright, so now it's inserted into the database, we have that new product there, now let's see about changing it. We have "product.name" equals "Sofa." So now we know that it's changed. And we could ask just the object itself, "Hey, have you changed?" It'll say, "Yes." The object has changed. The whole thing, all of the attributes, one of those attributes somewhere changed. And we can also ask for the specific changes like this, we saw that in the last movie.
But here we're just asking the object as a whole. If we want to get specific and just ask the attribute itself we'd ask "product.name_changed?" It says, "Yes, as a matter of fact, the name has changed." "Product.quantity_sold_changed?" It says, "Nope, that didn't change." So we can query specific attributes. We can also ask, "product.name_change," and it'll come back and tell us what the changes were, from "Couch" to "Sofa," or we can do "product.name_was" and that's handy to find out the previous value.
The current value, of course, is just "product.name." And then, we can also ask if the name changed, and I'm going to hit the up arrow a few times here, name changed, but I'm going to ask it, "Did it change from 'Couch'?" There we go. We're just going to do that alone. It says, "Yes, it did change from 'Couch.'" I can ask it, "Did it change to...'Sofa?'" There we go. It comes back and says, "Yes it did." Or I can combine those two together, "from 'Couch' to "Sofa?'" Yes, that's true.
Or I can ask, "Did it change from 'Couch' to 'Loveseat'?" No, that's not true. So we can ask for specific things. Because the active record code for dirty objects keeps track of the different changes that are going to be made and to which attributes they're going to be made, we can query our object to find about the state of things, we can find out if the object has changes, and we can drill down to specific attributes and find out if they have changes.
- Migrating databases
- Creating, updating, and deleting records
- Tracking changes to objects and attributes
- Writing queries
- Delegating attributes
- Working with associations