Forum: Ruby on Rails undefined method `count' for Array...

A07f0662e96a632d9447a710adadbe05?d=identicon&s=25 phil (Guest)
on 2008-08-13 09:48
(Received via mailing list)
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?
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2008-08-13 09:54
(Received via mailing list)
Try User.count or User.all.size.
A07f0662e96a632d9447a710adadbe05?d=identicon&s=25 phil (Guest)
on 2008-08-13 10:01
(Received via mailing list)
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!
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2008-08-13 10:06
(Received via mailing list)
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?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-08-13 10:11
(Received via mailing list)
On Aug 13, 9:00 am, phil <p...@philsmy.com> 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
A07f0662e96a632d9447a710adadbe05?d=identicon&s=25 phil (Guest)
on 2008-08-13 10:36
(Received via mailing list)
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 Cheung <frederick.che...@gmail.com>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-08-13 10:45
(Received via mailing list)
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
A10ba5e890803847d0ee59e178dfd8a3?d=identicon&s=25 Jeremy Olliver (Guest)
on 2008-08-13 13:57
(Received via mailing list)
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)
A07f0662e96a632d9447a710adadbe05?d=identicon&s=25 phil (Guest)
on 2008-08-13 14:34
(Received via mailing list)
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
9ac298c351adc1f871803ba1e3f6863d?d=identicon&s=25 James Brennan (jbrennan)
on 2009-11-17 20:53
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.
37512e7a7f034880c112f35bcf8e9c21?d=identicon&s=25 Joseph Pawletko (jgpawletko)
on 2009-12-23 17:52
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
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-12-23 18:04
Ryan Bigg 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.