This project is archived and is in readonly mode.
Add limit functionality to find first and last
-
Stephen Celis
- Tag changed from activerecord, finder, find_first, find_last, limit to activerecord, finder, find_first, find_last, limit, patch
-
Anil Wadghule
Automatic cleanup of spam.
-
Aditya Sanghi
Automatic cleanup of spam.
-
Aditya Sanghi
Automatic cleanup of spam.
-
Aditya Sanghi
- State changed from new to resolved
@Stephen, this functionality is now already in place in Rails 3.0.0. Marking this ticket as resolved. Feel free to comment, if you think otherwise.
-
Stephen Celis
Does not appear to be in place in 3.0.0:
% rails c Loading development environment (Rails 3.0.0) ruby-1.9.2-p0 > User.first 2 User Load (0.2ms) SELECTusers. FROMusers=> [] ruby-1.9.2-p0 > User.last 2 User Load (0.3ms) SELECTusers. FROMusers=> [] -
Stephen Celis
That is, the SQL query is not limited. Please re-open or change to a different state than "resolved."
-
Aditya Sanghi
@stephen, patch also does not apply cleanly anymore since there has been much refactoring around ARel.
I misread the second half of your OP and looked on the net result ( which works ) but the query does not since it does not use limit but gets all and then pick n from array.
Feel free to submit another patch.
-
Elad Meidar
Makes sense that the Rails default would fetch all and select back n accordingly, proper SQL query should be implemented on the adapter level since it involves different syntax for each DBMS.
-
Stephen Celis
I'm happy to fix the patch if there's interest in getting this into Rails.
@Elad, I'm not sure what you're saying. The Rails default for ActiveRecord::Base.first is to fetch one record from the database (with :limit => 1). As soon as you add a numeric argument, however, it fetches all, which is not what I would have expected. The only case I can think of where .first(n) would fetch all is ActiveRecord::Base.all.first(n), because .all returns an Array.
-
Ryan Bigg
Automatic cleanup of spam.
-
Piotr Sarnacki
I would add such functionality, anyone have anything against it?
