Delegated attributes require a related object by default, but they can also be configured to allow the object to be missing and to return nil as a value instead.
- [Narrator] In this movie, we will learn how delegations…are handled when there is no related object.…Let's begin by going into our rails console…and seeing the way that it works currently.…If you remember, we have already…delegated prefix to category,…so if we have a product that exists in the database,…product.first, that product already has a category.…Product.category, and we can ask it for product.prefix.…It delegates that value to that associated category.…
Okay, so now let's clear our screen,…and let's try another example.…Let's do product equals product.new.…It is a new object.…I have not assigned a category to it.…Product.category comes back with nil.…There is no category.…So when I ask for product.prefix, what's going to happen?…It's going to ask the product object for the prefix,…it's going to see that delegation,…the delegation is going to, say, in turn,…ask the associated category object for the result.…
And we get back an error.…Let's go back, take a look at the error.…It is a delegation error.…Product prefix delegated to category.prefix,…
- Migrating databases
- Creating, updating, and deleting records
- Tracking changes to objects and attributes
- Writing queries
- Delegating attributes
- Working with associations
Skill Level Advanced
Ruby on Rails 5 Essential Trainingwith Kevin Skoglund10h 21m Beginner
1. Database Migrations
2. CRUD Methods
3. Dirty Objects
4. Query Interface
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
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.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.