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.

Named scopes should implement #many? so they'll use COUNT(*)

#1500

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.

Reported by Chris Kampmeier · December 1st, 2008 @ 06:15 AM

State: resolved
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Paweł Kondzior
  2. Pratik
    Pratik
    • 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.

    December 22nd, 2008 @ 03:55 AM

  3. Chris Kampmeier
    Chris Kampmeier
    • 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.

    March 16th, 2009 @ 01:14 PM

  4. Pratik
    Pratik
    • State changed from incomplete to open

    March 16th, 2009 @ 01:17 PM

  5. Tarmo Tänav
    Tarmo Tänav

    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?

    May 11th, 2009 @ 09:29 AM

  6. Chris Kampmeier
    Chris Kampmeier

    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 to Enumerable 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.

    May 11th, 2009 @ 05:06 PM

  7. Repository
    Repository
    • State changed from open to resolved

    May 17th, 2009 @ 05:56 PM