<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,5 +1,15 @@
 *CVS*
 
+* Added a better exception for when a type column is used in a table without the intention of triggering single-table inheritance. Example:
+
+    ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'bad_class!'.
+    This error is raised because the column 'type' 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 Company.inheritance_column to use another column for that information.
+
+* Added that single-table inheritance will only kick in if the inheritance_column (by default &quot;type&quot;) is present. Otherwise, inheritance won't
+  have any magic side effects.
+
 * Added the possibility of marking fields as being in error without adding a message (using nil) to it that'll get displayed wth full_messages #208 [mjobin] 
 
 * Fixed Base.errors to be indifferent as to whether strings or symbols are used. Examples:</diff>
      <filename>activerecord/CHANGELOG</filename>
    </modified>
    <modified>
      <diff>@@ -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>
      <filename>activerecord/lib/active_record/associations.rb</filename>
    </modified>
    <modified>
      <diff>@@ -6,6 +6,8 @@ require 'yaml'
 module ActiveRecord #:nodoc:
   class ActiveRecordError &lt; StandardError #:nodoc:
   end
+  class SubclassNotFound &lt; ActiveRecordError #:nodoc:
+  end
   class AssociationTypeMismatch &lt; ActiveRecordError #:nodoc:
   end
   class SerializationTypeMismatch &lt; 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, 
+              &quot;The single-table inheritance mechanism failed to locate the subclass: '#{record[inheritance_column]}'. &quot; +
+              &quot;This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. &quot; +
+              &quot;Please rename this column if you didn't intend it to be used for storing the inheritance class &quot; +
+              &quot;or overwrite #{self.to_s}.inheritance_column to use another column for that information.&quot;
+            )
+          end
+
           object.instance_variable_set(&quot;@attributes&quot;, record)
           return object
         end</diff>
      <filename>activerecord/lib/active_record/base.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,4 +1,10 @@
 class Project &lt; ActiveRecord::Base
   has_and_belongs_to_many :developers, :uniq =&gt; true
   has_and_belongs_to_many :developers_named_david, :class_name =&gt; &quot;Developer&quot;, :conditions =&gt; &quot;name = 'David'&quot;, :uniq =&gt; true
+end
+
+class SpecialProject &lt; Project
+  def hello_world
+    &quot;hello there!&quot;
+  end
 end
\ No newline at end of file</diff>
      <filename>activerecord/test/fixtures/project.rb</filename>
    </modified>
    <modified>
      <diff>@@ -3,7 +3,7 @@ class Topic &lt; ActiveRecord::Base
   serialize :content
   
   before_create  :default_written_on
-  before_destroy :destroy_children #'self.class.delete_all &quot;parent_id = #{id}&quot;'
+  before_destroy :destroy_children
 
   def parent
     self.class.find(parent_id)</diff>
      <filename>activerecord/test/fixtures/topic.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,20 +1,13 @@
 require 'abstract_unit'
 require 'fixtures/company'
-
+require 'fixtures/project'
 
 class InheritanceTest &lt; Test::Unit::TestCase
-  def setup
-    @company_fixtures = create_fixtures &quot;companies&quot;
-  end
+  fixtures :companies, :projects
 
-  def switch_to_alt_inheritance_column
-    # we don't want misleading test results, so get rid of the values in the type column
-    Company.find_all(nil, &quot;id&quot;).each do |c|
-	    c['type'] = nil
-      c.save
-    end
-    
-    def Company.inheritance_column() &quot;ruby_type&quot; end
+  def test_a_bad_type_column
+    Company.connection.insert &quot;INSERT INTO companies (id, type, name) VALUES(100, 'bad_class!', 'Not happening')&quot;
+    assert_raises(ActiveRecord::SubclassNotFound) { Company.find(100) }
   end
 
   def test_inheritance_find
@@ -122,4 +115,21 @@ class InheritanceTest &lt; Test::Unit::TestCase
     switch_to_alt_inheritance_column
     test_complex_inheritance
   end
+
+  def test_inheritance_without_mapping
+    assert_kind_of SpecialProject, SpecialProject.find(1)
+    assert_nothing_raised { SpecialProject.create(&quot;name&quot; =&gt; &quot;And breaaaaathe!&quot;) }
+    
+  end
+
+  private
+    def switch_to_alt_inheritance_column
+      # we don't want misleading test results, so get rid of the values in the type column
+      Company.find_all(nil, &quot;id&quot;).each do |c|
+        c['type'] = nil
+        c.save
+      end
+    
+      def Company.inheritance_column() &quot;ruby_type&quot; end
+    end
 end
\ No newline at end of file</diff>
      <filename>activerecord/test/inheritance_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>317f13c2a8c047b3868a58e0121453b092557dba</id>
    </parent>
  </parents>
  <author>
    <name>David Heinemeier Hansson</name>
    <email>david@loudthinking.com</email>
  </author>
  <url>http://github.com/rails/rails/commit/605bc77533cf3b6700e91eda8994cdf6b82341cc</url>
  <id>605bc77533cf3b6700e91eda8994cdf6b82341cc</id>
  <committed-date>2004-12-14T04:32:29-08:00</committed-date>
  <authored-date>2004-12-14T04:32:29-08:00</authored-date>
  <message>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 &quot;type&quot;) 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</message>
  <tree>687a1c7e3bd319be5c0a6090dea190d6a8778658</tree>
  <committer>
    <name>David Heinemeier Hansson</name>
    <email>david@loudthinking.com</email>
  </committer>
</commit>
