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.

NamedScope#size doesn't respect limit options, if the collection isn't loaded

#2729

Given a named scope with a limit option

named_scope :limited_to_2, lambda { {:limit => 2} }

calling #size when the collection isn't loaded yet

Model.limited_to_2.size

will ignore the limit option and return the complete count of all stored Model instances.

See http://gist.github.com/118690 for a ready-to-run test case.

The fix would be:

class ActiveRecord::NamedScope::Scope
  def size
    return @found.length if @found
    
    @proxy_options[:limit] ? [@proxy_options[:limit],count].min : count
  end
end

Reported by niko · May 27th, 2009 @ 04:46 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. niko
    niko
    • Tag changed from active_record, named_scope to active_record, named_scope, patch

    May 28th, 2009 @ 07:53 AM

  2. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  3. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to stale
    • Importance changed from to

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 9th, 2010 @ 04:06 AM