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.

ModelName.limit(n).size returns invalid number

#5183

ModelName.limit(n).size returns invalid number

Question.limit(3).size
=> 200

Question.limit(3).length
=> 3

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

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. lakshmanan
  2. Stevo
    Stevo

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

    July 24th, 2010 @ 02:40 PM

  3. lakshmanan
    lakshmanan

    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
    

    July 24th, 2010 @ 02:51 PM

  4. lakshmanan
    lakshmanan
    • Tag changed from limit rails3 arel, scope to limit rails3 arel, invalid, scope

    July 24th, 2010 @ 02:54 PM

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

    July 24th, 2010 @ 03:46 PM

  6. Stevo
    Stevo

    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)

    July 24th, 2010 @ 04:13 PM

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

    July 25th, 2010 @ 02:30 AM

  8. Stevo
    Stevo

    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

    July 26th, 2010 @ 08:05 AM

  9. Stevo
    Stevo

    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

    July 26th, 2010 @ 08:12 AM

  10. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 04:35 PM

  11. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:35 PM