This project is archived and is in readonly mode.

#1429 ✓invalid
Chris O'Sullivan

Add singular version of named_scope that returns first object

Reported by Chris O'Sullivan | November 21st, 2008 @ 05:24 PM | in 2.x

So, I find myself creating a bunch of named_scopes designed to return only one object, like this:

class Book
  named_scope :most_recently_read, :order => "last_read desc", :limit => 1

Which means I have to chain .first to get what I want like this:


Which just feels plain wrong!

My proposition is adding some way of creating a named_scope that is designed to return only one object.

I've attached two patches (with docs and tests) for this. The first one (add_first_to_named_scope.patch) adds a first option to named_scope, so it looks like this:

named_scope :most_recently_read, :first => true, :order => "last_read desc", :limit => 1

But I think the :first => true format looks pretty ugly.

My second patch (add_singular_named_scope.patch) proposition is to add a new method singular_named_scope. You would define your singular scopes thusly:

singular_named_scope :most_recently_read, :order => "last_read desc", :limit => 1

But..I'm open to suggestions!

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

Shared Ticket Bins