Recently, I stumbled upon a new Rails feature called named_scope.

Inspired by this feature I wrote — just for fun — a small behavior which allows you to define named finder methods (download).

Let’s say we want to define a named finder method to get the most recent posts. The code for it is quite straightforward (you can use the same options as for the built-in find method):

// in the Post model
public $actsAs = array('NamedFinder' => array('recent' => array('order' => 'Post.created DESC', 'limit' => 10)));

With this definition in place, you can then use the named finder method in the following way:

// in your controller (e.g. PostsController)
$this->Post->findRecent();

You can also combine them with “And” if you defined multiple named finders:

$this->Post->findRecentAndPublished();

I’m not sure if this behavior is really useful in practice, as it is much less powerful than the named_scope feature of Rails, and you can accomplish (almost) the same with custom model methods or by defining a custom find type