# Undefined method \`count' for Array

**URL:** <https://www.ruby-forum.com/t/undefined-method-count-for-array/145718>\
**Category:** Rails\
**Created:** [August 13, 2008, 9:48am UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718 "2008-08-13T09:48:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 9:48am UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718/1 "2008-08-13T09:48:46Z")

</div>

I am experiencing something quite weird… it must come from my  
environment, but I am not sure how to track it down.

I have this bit of code:

User.all.count

In my dev environment I get this;

script/console  
Loading development environment (Rails 2.1.0)  
\*\* Loading custom logger format for rawk

> > User.all.count  
> > =\> 2

in production:

script/console production  
Loading production environment (Rails 2.1.0)  
\*\* Loading custom logger format for rawk

> > User.all.count  
> > NoMethodError: undefined method `count’ for #Array:0xb5ca8874  
> > from (irb):1

Both are running in the same project, with the same frozen Rails, gems  
and plugins… but there must be something in my dev environment that  
is giving me this… any ideas? Is there any way to find out where an  
object is getting a method from?

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 9:54am UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718/2 "2008-08-13T09:54:02Z")

</div>

Try User.count or User.all.size.

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 10:01am UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718/3 "2008-08-13T10:01:04Z")

</div>

Thanks Ryan,

I know that those will work, I am just curious (i.e. concerned) about  
the differences in environments. I am not looking for how to get a  
total of Users… I want to know where that count method comes from!

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 10:11am UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718/4 "2008-08-13T10:11:37Z")

</div>

On Aug 13, 9:00 am, phil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Ryan,
> 
> I know that those will work, I am just curious (i.e. concerned) about  
> the differences in environments. I am not looking for how to get a  
> total of Users… I want to know where that count method comes from!

use rdebug to step into the count method. Activerecord adds a count  
method to association proxies (which look like Arrays but aren’t) and  
so on as well (but not actual arrays)

Fred

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 10:06am UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718/5 "2008-08-13T10:06:20Z")

</div>

The count method should’ve never existed on Array afaik. For example  
if you do:

[1,2,3,4].count you should get an undefined method error.

Same if you do it in irb. I have no idea where you’re getting that  
count method from! Tried looking through your source for it?

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 10:45am UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718/6 "2008-08-13T10:45:34Z")

</div>

On 13 Aug 2008, at 09:36, phil wrote:

> Um… good idea, if I knew anything about ruby debug… how do I step  
> into count? Sorry for the newb questions.

ifd  
Assuming you’ve got the ruby-debug gem installed then in script/console:

require ‘ruby-debug’  
def test\_method  
debugger  
User.all.count  
end  
test\_method

at which point you should drop into the debugger. e to evaluate a ruby  
expression, s to step, l to show you the code you’re stepping through,  
help to see more commands.  
(typing l to begin with might not work - seems like rdebug doesn’t  
like showing the source if the source was just part of an irb context  
and not an actual file.

Fred

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718/7 "2008-08-13T13:57:52Z")

</div>

The count method comes from active record, it fits it’s at the same  
level as the .find and .find\_by etc methods. When you call .find(:all)  
you’re getting back an array, not an active record association, so you  
can’t call count on this.  
Ryan is correct that User.count(:all) or User.all.size should be used  
(.all returns the array)

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 10:36am UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718/8 "2008-08-13T10:36:47Z")

</div>

Um… good idea, if I knew anything about ruby debug… how do I step  
into count? Sorry for the newb questions.

On Aug 13, 10:10 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [November 17, 2009, 8:53pm UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718/9 "2009-11-17T20:53:34Z")

</div>

You’re probably seeing this error because ruby 1.8.7 introduced count as  
a method on Array, whereas it does not exist in 1.8.6. Chances are  
production is running 1.8.6 or earlier.

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [December 23, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718/10 "2009-12-23T17:52:17Z")

</div>

James Brennan wrote:

> You’re probably seeing this error because ruby 1.8.7 introduced count as  
> a method on Array, whereas it does not exist in 1.8.6. Chances are  
> production is running 1.8.6 or earlier.

Thank you for your post. That’s exactly what happened to me:  
dev: 1.8.7  
prod: 1.8.6

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 13, 2008, 2:34pm UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718/11 "2008-08-13T14:34:13Z")

</div>

thanks Jeremy… still doesn’t explain why the code actually works  
fine in my dev environment. I think it has something to do with  
named\_scopes. User.all is a named\_scope I believe.

Production:  
script/console production  
Loading production environment (Rails 2.1.0)

> > User.all.class  
> > =\> Array  
> > User.find(:all).class  
> > =\> Array  
> > User.find(:all).count  
> > NoMethodError: undefined method `count’ for #Array:0xb5c7819c  
> > from (irb):3

Development:  
script/console  
Loading development environment (Rails 2.1.0)

> > User.all.class  
> > =\> Array  
> > User.find(:all).class  
> > =\> Array  
> > User.find(:all).count  
> > =\> 2

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [December 23, 2009, 6:04pm UTC](https://www.ruby-forum.com/t/undefined-method-count-for-array/145718/12 "2009-12-23T18:04:44Z")

</div>

Ryan B. wrote:

> Try User.count or User.all.size.

Unless I missed it it seems that no one actually mentioned the real  
difference between these two calls. While they will produce the same end  
result the process of getting to that result is very different:

n = User.all.size  
=\> 123  
SELECT \* FROM “users”

n = User.count  
=\> 123  
SELECT count(\*) AS count\_all FROM “users”

Obviously the latter is significantly more efficient.
