This project is archived and is in readonly mode.

#5183 ✓ stale
Stevo

ModelName.limit(n).size returns invalid number

Reported by Stevo | July 23rd, 2010 @ 09:49 AM

ModelName.limit(n).size returns invalid number

Question.limit(3).size
=> 200

Question.limit(3).length
=> 3

Comments and changes to this ticket

  • lakshmanan
  • Stevo

    Stevo July 24th, 2010 @ 02:40 PM

    Still, isn't it counter-intuitive that size() executed on scope counts all records disregarding limit() condition?

  • lakshmanan

    lakshmanan July 24th, 2010 @ 02:51 PM

    size() - returns the count of all the records in the table if the ActiveRecord::Relation is not loaded. thats why you got 200 (the count)

    see http://github.com/rails/rails/blob/master/activerecord/lib/active_r... line 81

    
    # in rails source code
    
    def size
          loaded? ? @records.length : count
    end
    
  • lakshmanan

    lakshmanan July 24th, 2010 @ 02:54 PM

    • Tag changed from limit rails3 arel, scope to limit rails3 arel, invalid, scope
  • Manik

    Manik July 24th, 2010 @ 03:46 PM

    • Assigned user set to “Pratik”

    Lazy loading might be causing some issue with the first call

    Question.limit(3).all.size should return what you need. [Not sure though why anybody would want to call that]

    Not sure why length works correctly.

  • Stevo

    Stevo July 24th, 2010 @ 04:13 PM

    I just did want to make myself sure, that there was at least n records returned (in example that I gave it doesn't make sense, however there were more scopes in original code)

  • Rohit Arondekar

    Rohit Arondekar July 25th, 2010 @ 02:30 AM

    • Assigned user cleared.
    • Tag changed from limit rails3 arel, invalid, scope to limit rails3 arel, scope
    • Importance changed from “” to “Low”

    Stevo, did you try this on Rails master? It works fine for me on master:

    Post.all.size
     => 4 
    Post.all.length
     => 4 
    Post.limit(3).size
     => 3 
    Post.limit(3).length
     => 3
    
  • Stevo

    Stevo July 26th, 2010 @ 08:05 AM

    rails -v
    Rails 3.0.0.beta4
    ruby -v
    ruby 1.8.7 (2009-12-24 patchlevel 248) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2010.01

  • Stevo

    Stevo July 26th, 2010 @ 08:12 AM

    Just tested it against some other model to make sure that it is not model specific somehow...

    >>User.count
     => 2 
    >>User.limit(1).count
     => 2 
    >>User.limit(1).size
     => 2 
    >>User.limit(1).length
     => 1
    

    Edited by Rohit Arondekar for formating

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:35 PM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:35 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Tickets have moved to Github

The new ticket tracker is available at https://github.com/rails/rails/issues

Shared Ticket Bins

Pages