This project is archived and is in readonly mode.

#2729 ✓stale
niko

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

Reported by niko | May 27th, 2009 @ 04:46 PM | in 3.x

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

Comments and changes to this ticket

  • niko

    niko May 28th, 2009 @ 07:53 AM

    • Tag changed from active_record, named_scope to active_record, named_scope, patch
  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Rohit Arondekar

    Rohit Arondekar October 9th, 2010 @ 04:06 AM

    • 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.

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>

Pages