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

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Solution: Callbacks

Solution: Callbacks - Ruby on Rails Tutorial

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

Start my 1-month free trial

Solution: Callbacks

(upbeat music playing) - [Instructor] I hope that you were able to complete this challenge assignment. In this movie, I'm going to show you the solution that I came up with. Your first task, was to add a validation, to both subject and page, that before it validates will, titleize the name. In the subject class, I have before_validation :titleize name. So now I've registered my callback. And then down here, I have a method called titleize_name. And what does it do? It sets name equal to whatever the value of name is, and calls titleize on it. Two things I want to note here. The first is that I typically try to put my validations kind of in order. So something that's a before validation, I would put at the top, whereas something that's after commit, I would put towards the bottom. And also notice that I'm using self when I'm setting the value, but I'm not using self when I'm calling the value. That's a basic…

Contents