classTasksController < ApplicationControllerdefindex@tasks = Task.find_incomplete
This is a little limiting as we can’t add conditions to the custom find method to find, say, the first twenty incomplete tasks. What we want to do is something like
@tasks = Task.find_incomplete :limit => 20
One way to do this is to add an options hash to the find_incomplete method in the model and merge it with the options in the find, but a more elegant solution exists with find_scope, passing the find options.
Any find executed within the with_scope block will automatically inherit the specified options. Now the find_incomplete can take any conditions passed. This will also work within another scope. Our custom method is called in the ProjectsController. We can pass a limit condition here as well. This find passes through two scopes: first the incomplete tasks are found within the scope of the specified project and secondly they are limited to the first twenty.