Default scopes

- [Instructor] In this movie,…we will learn how to add Default Scopes…to our active record models.…First, let's review basic named scopes.…When we want to find a named scope inside one of our models,…we simply write scope, and then the name of the scope,…the name we want to assign to it,…and then inside a lambda, inside those curly braces,…we're going to say what the active record query is…that we want to add as our scope.…Here I'm using a where clause,…but we could also use order, or limit, or offset,…any of those things that we've learned how to use,…could all go here.…

And we can chain them together too.…The one I've got, is simply checking to see…if active is true.…So, if I call User.active,…then it's going to return to me a list of all users…where active is true.…Active just encapsulates that behavior as a named scope.…Okay, so that's the named scope.…A default scope works the exact same way,…but as the name suggests, it's there by default.…So I have default_scope and then in curly braces,…I've got that same where active is true.…

Resume Transcript Auto-Scroll

Author

Released

1/27/2017

Ready to speed up Ruby on Rails development? Understanding how ActiveRecord is used to create, update, and retrieve data in a relational database is fundamental to writing more efficient, object-oriented code. This course helps you understand the basics of ActiveRecord and leverage shortcuts and techniques to make data migrations faster and more powerful. Kevin Skoglund reviews the basics of working with database records, and shows how to use ActiveRecord queries to locate records faster. He also shows how to use callbacks to automate functionality when objects are saved or deleted, and create associations between ActiveRecord models to streamline common operations. By the end of the course, you will have the skills you need to write concise, high-performance Rails code that eliminates the need to deal with SQL and other tedious database queries.