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.

Allow limit option on find last to retrieve many recods

#1430

find(:last) (or just last) ignores the :limit option.

This was because of two reasons, returning only the last record matching the query and make use of find_initial.

Sometimes i need to query for the last X records created, for example: "Last 5 comments on this product..."

With this small patch you'll be able to do:

Product.find(:last, :limit => 5)

In order to retrieve those 5 products.

Reported by Emilio Tagua · November 21st, 2008 @ 09:47 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. CancelProfileIsBroken
    CancelProfileIsBroken

    The syntax worries me a bit. It's not immediately obvious to me whether find(:last, :limit => 5) means "find the last five in the entire model" or "find a maximum of five instances and give me the last of those".

    November 21st, 2008 @ 10:29 PM

  2. Emilio Tagua
    Emilio Tagua

    Hey Mike,

    The syntax is the same as for find(:all) and i guess you are not worried of doing find(:all, :limit =>5).

    Also find_initial (or first) is just a find(:all) (or find_every) with :limit => 1.

    November 21st, 2008 @ 10:35 PM

  3. DHH
    DHH
    • State changed from new to wontfix

    Gotta say that I'm not a fan. find(:last) is a mirror of find(:first), not find(:all).

    November 23rd, 2008 @ 05:37 PM