Callbacks can be configured so they run only when certain conditional statements have been met.
- [Voiceover] In this movie,…we'll learn about conditional callbacks,…or callbacks which run only under certain circumstances.…In order to make a conditional callback,…what we need to do is add those conditions…to the callback registration methods.…And we do that by adding options to that registration.…We use :if and :unless options.…So, for example, let's say we had an after_save callback,…whose method it called was :rejoice,…then we could put a comma, and then as an option…we could use either :if or :unless…and then the value that goes with that option…is going to be either a method or a code block.…
And that method or code block…we would expect to return true or false,…because that's what we're doing,…we're evaluating a Boolean.…Should this callback be run or not?…Well it will only be run…if that method or block returns true.…Or, in the case of :unless, it will only run…if that method or block returns false.…So let's look at an example.…In the last movie, we registered…a before_validation callback on our category object.…
Author
Released
1/27/2017- Migrating databases
- Creating, updating, and deleting records
- Tracking changes to objects and attributes
- Writing queries
- Delegating attributes
- Callbacks
- Working with associations
Skill Level Intermediate
Duration
Views
Related Courses
-
Ruby: Testing with RSpec
with Kevin Skoglund6h 13m Intermediate -
Ruby on Rails 5 Essential Training
with Kevin Skoglund10h 21m Beginner -
Ruby Essential Training
with Kevin Skoglund6h 57m Beginner
-
Introduction
-
Welcome1m
-
-
1. Database Migrations
-
Create a database5m 43s
-
Migration shortcuts8m 19s
-
The change method4m 27s
-
Write reversible code4m 10s
-
Irreversible migrations4m 39s
-
Revert migrations4m 21s
-
Create a join table4m 38s
-
Control migration output7m 39s
-
Dump the database schema4m 52s
-
Define seed data4m 52s
-
-
2. CRUD Methods
-
Create records with a block2m 29s
-
Update multiple records5m 56s
-
Delete multiple records3m 24s
-
Touch records3m 47s
-
Toggle attribute values2m 10s
-
-
3. Dirty Objects
-
Track changes to objects5m 6s
-
Track changes to attributes4m 43s
-
Restore attributes4m 6s
-
-
4. Query Interface
-
Other find methods8m 2s
-
Select partial record data5m 31s
-
Calculations5m 15s
-
Negative queries3m 20s
-
Or queries46s
-
Retrieve records in batches5m 43s
-
Default scopes6m 49s
-
Join tables8m 13s
-
Distinct records4m 42s
-
Left join tables6m 2s
-
Eager loading9m 7s
-
-
5. Delegations
-
Delegate attributes8m 34s
-
Set a prefix2m 13s
-
Without a related object3m 26s
-
-
6. Callbacks
-
Callback basics8m 35s
-
Conditional callbacks3m 52s
-
Around callbacks4m 41s
-
Other callbacks and tips4m 6s
-
-
7. Associations
-
Review3m 28s
-
Destroy associated records7m 52s
-
Touch associated records5m 47s
-
Counter caches9m 39s
-
Single table inheritance11m 49s
-
Polymorphic associations5m 34s
-
-
Conclusion
-
Next steps21s
-
- 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.
CancelTake 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.
Share this video
Embed this video
Video: Conditional callbacks