Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm getting "undefined method `abstract_class?' for Object:Class" for count_by_sql

I'm getting the error: undefined method 'abstract_class?' for Object:Class

on a count_by_sql as below:

my_count = ActiveRecord::Base.count_by_sql(["SELECT widgets FROM wodgets WHERE colour = ? LIMIT 1", my_favourite_colour])

I've just been upgrading from Rails 2.2.2 to 2.3.4 and it used to work before.

like image 973
Taryn East Avatar asked Aug 24 '10 11:08

Taryn East


1 Answers

ActiveRecord's count_by_sql calls some deeper ActiveRecord::Base magic that assumes you are an actual ActiveRecord (ie something that inherits from AR, not AR itself) and thus tries to call an internal method called abstract_class? that would normally return the class name (eg Order or Product).

You can get around this by using an actual AR object (it doesn't matter which one) eg:

my_count = MyWidget.count_by_sql(["SELECT widgets FROM wodgets WHERE colour = ? LIMIT 1", my_favourite_colour])
like image 141
Taryn East Avatar answered Nov 15 '22 08:11

Taryn East