From 53f18f2c5475809f7f6c5576aba89d6340a57c06 Mon Sep 17 00:00:00 2001 From: Aaron Weiner Date: Wed, 27 Feb 2013 18:17:29 -0500 Subject: More helpful error message when instantiating an abstract class Calling a literal ActiveRecord::Base.new raises NoMethodError, since it ends up calling Class.abstract_class? which does not exist. Similarly, instantiating an actual abstract class hits the database, when conventionally it should immediately throw NotImplementedError. ActiveRecord::Base can't be made abstract without breaking many, many things, so check for it separately. --- activerecord/lib/active_record/inheritance.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'activerecord/lib/active_record/inheritance.rb') diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb index e630897a4b..13a8352f13 100644 --- a/activerecord/lib/active_record/inheritance.rb +++ b/activerecord/lib/active_record/inheritance.rb @@ -15,6 +15,9 @@ module ActiveRecord # and if the inheritance column is attr accessible, it initializes an # instance of the given subclass instead of the base class def new(*args, &block) + if abstract_class? || self == Base + raise NotImplementedError, "#{self} is an abstract class and can not be instantiated." + end if (attrs = args.first).is_a?(Hash) if subclass = subclass_from_attrs(attrs) return subclass.new(*args, &block) -- cgit v1.2.3