Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Use SQL for first and last on association proxy

#226

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.

Reported by Ryan Bates · May 19th, 2008 @ 09:57 PM

State: resolved
Milestone: 2.1.1
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: none

Activity

  1. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 2.1.1
    • Assigned user set to Jeremy Kemper

    Hey Ryan, looks good but the patch doesn't apply cleanly to latest master. Could you update?

    May 20th, 2008 @ 03:24 AM

  2. Pratik
    Pratik
    • State changed from new to resolved

    May 20th, 2008 @ 12:29 PM