In Ruby on Rails 5, the belongs_to relationship is not optional by default. When “belongs_to” is added to a model, it also adds a validation to ensure that the associated object is present. If it is not present then the record will be considered invalid and will not save. This behavior can be disabled if necessary by adding “:optional => true” to the model.
- In this movie, we're gonna talk about the…belongs to presence validation, to make sure…that it doesn't become a problem for you.…Belongs to presence validation is a change in Rails 5.0,…It didn't exist before that.…Prior to Rails 5.0 an object that had a…belongs to relationship could be saved to the…database even if it didn't have a parent object…associated with it.…That is the belongs to relationship was not required…To be present.…Now, starting in Rails 5,…the belongs to relationship is not optional by default,…what happens behind the scenes is that when we…declare a belongs to, active record adds a validation…on the object for the related objects…presence...…
The parent object.…So if we have our page that means, that the page…won't save to the database.…Unless we have a subject associated with it.…Now we're gonna talk more about validations later.…But all that you really need to know for now,…is that a validation is there to check and make sure…that the data is correct before allowing the record to be…saved to the database.…
- Creating and configuring a new Ruby on Rails project
- Generating controllers and views
- Handling server requests
- Using different types of routes
- Rendering and viewing templates
- Generating migrations and models
- Creating, updating, and deleting records
- Finding records with queries
- Understanding relationship types
- Writing controllers for CRUD
- Working with layouts and helpers
- Managing application assets
- Building forms
- Validating data
- Authenticating users
Skill Level Beginner
1. What Is Ruby on Rails?
2. Get Started
3. Controllers, Views, and Dynamic Content
4. Databases and Migrations
5. Models and ActiveRecord
7. CRUD, REST, and Resourceful Routes
8. Controllers and CRUD
9. Layouts, Partials, and View Helpers
12. Data Validation
13. Controller Features
14. User Authentication
15. Improve the Simple CMS
Next steps1m 44s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.