From 605bc77533cf3b6700e91eda8994cdf6b82341cc Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 14 Dec 2004 12:32:29 +0000 Subject: Added a better exception for when a type column is used in a table without the intention of triggering single-table inheritance. Added that single-table inheritance will only kick in if the inheritance_column (by default "type") is present. Otherwise, inheritance wont have any magic side effects git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@149 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 4 +++- activerecord/lib/active_record/base.rb | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index c51210ef1f..ba7f34d729 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -581,7 +581,9 @@ module ActiveRecord end def require_association_class(class_name) - begin + return unless class_name + + begin require_association(Inflector.underscore(class_name)) rescue LoadError # Failed to load the associated class -- let's hope the developer is doing the requiring himself. diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 046819f7bb..899db1af10 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -6,6 +6,8 @@ require 'yaml' module ActiveRecord #:nodoc: class ActiveRecordError < StandardError #:nodoc: end + class SubclassNotFound < ActiveRecordError #:nodoc: + end class AssociationTypeMismatch < ActiveRecordError #:nodoc: end class SerializationTypeMismatch < ActiveRecordError #:nodoc: @@ -535,7 +537,7 @@ module ActiveRecord #:nodoc: end def descends_from_active_record? # :nodoc: - superclass == Base + superclass == Base || !columns_hash.has_key?(inheritance_column) end def quote(object) @@ -568,7 +570,20 @@ module ActiveRecord #:nodoc: # Finder methods must instantiate through this method to work with the single-table inheritance model # that makes it possible to create objects of different types from the same table. def instantiate(record) - object = record_with_type?(record) ? compute_type(record[inheritance_column]).allocate : allocate + require_association_class(record[inheritance_column]) + + begin + object = record_with_type?(record) ? compute_type(record[inheritance_column]).allocate : allocate + rescue NameError + raise( + SubclassNotFound, + "The single-table inheritance mechanism failed to locate the subclass: '#{record[inheritance_column]}'. " + + "This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " + + "Please rename this column if you didn't intend it to be used for storing the inheritance class " + + "or overwrite #{self.to_s}.inheritance_column to use another column for that information." + ) + end + object.instance_variable_set("@attributes", record) return object end -- cgit v1.2.3