This project is archived and is in readonly mode.
Optimize named_scope first and last to use LIMIT rather than loading the full collection
Reported by Lars Klevan | August 2nd, 2008 @ 07:02 PM | in 2.x
I wanted to use Account.recent.first(5) to display the most recent 5 accounts in the system. I noticed that this was loading the whole table into memory rather than using a LIMIT 5.
When I dug in I noticed that Account.first(5) returned only the first record.
Goal: make this two calls efficient and consistent with first and last on Array.
Comments and changes to this ticket
-
José Valim August 3rd, 2008 @ 11:18 AM
- Tag changed from activerecord, performance to activerecord, patch, performance
It sounds great for me!
+1
-
DHH September 10th, 2008 @ 06:32 AM
- State changed from new to incomplete
Looks really nice. Needs doc updates, though.
-
Lars Klevan September 15th, 2008 @ 11:24 PM
Updated the docs for ActiveRecord#first and #last. Example for #first below.
A convenience wrapper for find(:first, *args). You can pass in all the
same arguments to this method as you can to find(:first) If the first
argument is an integer an array of the first n records will be returned.
#
==== Examples
#
Person.first # Equivalent to Person.find(:first)
Person.first(:conditions => [ "user_name = ?", user_name])
Person.first(5) # Equivalent to Person.find(:all, :limit => 5)
Person.first(5, :conditions => [ "user_name = ?", user_name])
-
Pratik January 21st, 2010 @ 04:14 PM
- State changed from incomplete to resolved
No longer needed.
-
Jeff Kreeftmeijer October 12th, 2010 @ 09:06 AM
- Tag cleared.
- Importance changed from to Low
Automatic cleanup of spam.
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>