diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index de3a777920..d3d0b3d510 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Subclasses of an abstract class work with single-table inheritance. #5704, #7284 [BertG, nick+rails@ag.arizona.edu] + * Make sure sqlite3 driver closes open connections on disconnect [Rob Rasmussen] * [DOC] clear up some ambiguity with the way has_and_belongs_to_many creates the default join table name. #7072 [jeremymcanally] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index a783feafed..b8a497ffa3 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -817,8 +817,13 @@ module ActiveRecord #:nodoc: attribute_key_name.humanize end - def descends_from_active_record? # :nodoc: - superclass == Base || !columns_hash.include?(inheritance_column) + # True if this isn't a concrete subclass needing a STI type condition. + def descends_from_active_record? + if superclass.abstract_class? + superclass.descends_from_active_record? + else + superclass == Base || !columns_hash.include?(inheritance_column) + end end diff --git a/activerecord/test/fixtures/company.rb b/activerecord/test/fixtures/company.rb index c012a978a0..ff92b14fcb 100755 --- a/activerecord/test/fixtures/company.rb +++ b/activerecord/test/fixtures/company.rb @@ -1,4 +1,8 @@ -class Company < ActiveRecord::Base +class AbstractCompany < ActiveRecord::Base + self.abstract_class = true +end + +class Company < AbstractCompany attr_protected :rating set_sequence_name :companies_nonstd_seq diff --git a/activerecord/test/inheritance_test.rb b/activerecord/test/inheritance_test.rb index 8b0d70cfa3..7ab1d91061 100755 --- a/activerecord/test/inheritance_test.rb +++ b/activerecord/test/inheritance_test.rb @@ -6,6 +6,13 @@ require 'fixtures/subscriber' class InheritanceTest < Test::Unit::TestCase fixtures :companies, :projects, :subscribers, :accounts + def test_company_descends_from_active_record + assert_raise(NoMethodError) { ActiveRecord::Base.descends_from_active_record? } + assert AbstractCompany.descends_from_active_record?, 'AbstractCompany should descend from ActiveRecord::Base' + assert Company.descends_from_active_record?, 'Company should descend from ActiveRecord::Base' + assert !Class.new(Company).descends_from_active_record?, 'Company subclass should not descend from ActiveRecord::Base' + end + def test_a_bad_type_column #SQLServer need to turn Identity Insert On before manually inserting into the Identity column if current_adapter?(:SQLServerAdapter, :SybaseAdapter)