Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11413 (closed enhancement: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] Add alias methods .first and .last representing find(:first) and find(:last)

Reported by: thechrisoshow Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc:

Description

The new 'named scope' functionality introduced a named scope 'all'. The upshot of which is that we can now do the following:

Topic.all #=> The same as Topic.find :all

This patch adds alias methods for the find(:first) and find(:last) methods

Topic.first #=> The same as Topic.find :first
Topic.last #=> The same as Topic.find :last
Topic.first(:conditions => ['name = ?', 'Chris']) #=> The same as Topic.find(:first, :conditions => ['name = ?', 'Chris']) 

Patch includes docs and tests

Attachments

add_first_and_last_aliases.diff (2.3 kB) - added by thechrisoshow on 03/24/08 17:19:20.
Adds alias methods first and last for find(:first) and find(:last) docs and tests included

Change History

03/24/08 17:19:20 changed by thechrisoshow

  • attachment add_first_and_last_aliases.diff added.

Adds alias methods first and last for find(:first) and find(:last) docs and tests included

03/24/08 17:45:16 changed by wesley.moxam

Why all the sugar? Isn't AR sweet enough?

03/24/08 18:00:09 changed by thechrisoshow

Two reasons: 1) To be consistent with the new .all method (introduced with named scopes). I mean, if we have Topic.all users would expect there to be similar methods Topic.first and Topic.last

2) I'm a lazy sod, and constantly use Blah.find :first when testing stuff outin t/console. Now I can kiss goodbye to a whole 6 letters everytime - hooray!

03/24/08 19:59:25 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

(In [9085]) Added ActiveRecord#Base.all/first/last as aliases for find(:all/:first/:last) (closes #11413) [nkallen, thechrisoshow]