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?
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)
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.
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)
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)
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
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.