This project is archived and is in readonly mode.
Named scopes should implement #many? so they'll use COUNT(*)
Reported by Chris Kampmeier | December 1st, 2008 @ 06:15 AM | in 2.x
Enumerable#many?
went in recently, and it's nice to
have. However, many Enumerable
methods have
corresponding implementations in the
ActiveRecord::NamedScope::Scope
class, so that named
scopes will do the right thing w/ SQL instead of just loading an
entire collection and then sending the Enumerable
method.
Currently:
class User < ActiveRecord::Base
named_scope :first_half, :conditions => ["name < ?", "N"]
end
>> User.first_half.many?
# User Load (0.6ms) SELECT * FROM "users" WHERE (name < 'N')
=> true
With this patch:
>> User.first_half.many?
# SQL (0.1ms) SELECT count(*) AS count_all FROM "users" WHERE (name < 'N')
=> true
Much better!
Just like #any?
, it'll load the collection instead
of using COUNT(*)
when you use a block:
>> User.first_half.many? { |user| user.name.starts_with?('A') }
# User Load (0.4ms) SELECT * FROM "users" WHERE (name < 'N')
=> false
That's about it, let me know what you think.
Comments and changes to this ticket
-
Pratik December 22nd, 2008 @ 03:55 AM
- Assigned user set to Pratik
- State changed from new to incomplete
Looks good. Why only named scope though ? It should be implemented for AssocitionCollections too.
-
Chris Kampmeier March 16th, 2009 @ 01:14 PM
- Tag changed from enumerable, named_scope, patch to associations, enumerable, named_scope, patch
OK, here's an updated patch that implements
#many?
for AssociationCollections, too. -
Pratik March 16th, 2009 @ 01:17 PM
- State changed from incomplete to open
-
Tarmo Tänav May 11th, 2009 @ 09:29 AM
Wouldn't it be better to only fetch the primary keys for two records instead of counting them all just to find out if there is more than one?
-
Chris Kampmeier May 11th, 2009 @ 05:06 PM
Are you suggesting something like
SELECT [primary_key] FROM [table] ... LIMIT 2
?I'm not sure whether that would be better or worse -- it seems like a speed difference would be dependent on how the database's
COUNT
works. It would definitely make the implementation more complicated, though: you'd have to branch in the code for when the collection is already loaded, etc.This patch just adds an analog of
#empty?
and#any?
for#many?
, which probably shouldn't have been delegated toEnumerable
in the first place.If using
LIMIT 2
and such is an improvement, that would be great -- but for that kind of refactoring, I'd say a separate patch that affects all three would be more appropriate. -
Repository May 17th, 2009 @ 05:56 PM
- State changed from open to resolved
(from [4e8c36a7417e5d447c9b15d5c61df0c014ee6d3b]) Implement #many? for NamedScope and AssociationCollection using #size [#1500 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/4e8c36a7417e5d447c9b15d5c61df0...
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>
People watching this ticket
Attachments
Referenced by
- 1500 Named scopes should implement #many? so they'll use COUNT(*) (from [4e8c36a7417e5d447c9b15d5c61df0c014ee6d3b]) Impleme...