This project is archived and is in readonly mode.

#226 ✓resolved
Ryan Bates

Use SQL for first and last on association proxy

Reported by Ryan Bates | May 19th, 2008 @ 09:57 PM | in 2.1.1

We have support for first/last methods on a model class:

Project.first
Project.last(:order => 'name')

Wouldn't it be nice if this worked on association proxies as well? If a category has many products it would work like this:

category.products.first
category.products.last(:order => 'name')

While we're at it, let's make it work for named_scope too. Say we have a "recent" named scope, we can quickly fetch the first or last recent product like this:

Product.recent.first
category.products.recent.last

The attached patch will add the first/last methods to both the association proxy and scopes. Tests are included.

Note: I needed to change a couple of existing tests to get this to work because they relied on first/last loading the association target. However this was not the functionality being tested so I think it's acceptable.

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages