diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2012-01-12 20:32:11 +0100 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2012-01-12 20:53:01 +0100 |
commit | 0d1df723c79300b73691d7785ee87d7b74caf591 (patch) | |
tree | e4e60a3d73e0a633024c72f369bd2710f7de31f2 | |
parent | ac153fe4859ec32bdbb4294d5695af25180cb617 (diff) | |
download | rails-0d1df723c79300b73691d7785ee87d7b74caf591.tar.gz rails-0d1df723c79300b73691d7785ee87d7b74caf591.tar.bz2 rails-0d1df723c79300b73691d7785ee87d7b74caf591.zip |
Fix table_name in ActiveRecord with more than one abstract ancestors
When subclassing abstract_class table_name should be always computed
based on class name, no matter if superclass is subclassing base
or another abstract_class. So:
class FirstAbstract < ActiveRecord::Base
self.abstract_class = true
end
class SecondAbstract < FirstAbstract
self.abstract_class = true
end
class Post < SecondAbstract
self.table_name #=> 'posts' (not 'second_abstracts')
end
-rw-r--r-- | activerecord/lib/active_record/model_schema.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 11 |
2 files changed, 18 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb index 36417d89f7..0e8c2c09ce 100644 --- a/activerecord/lib/active_record/model_schema.rb +++ b/activerecord/lib/active_record/model_schema.rb @@ -139,10 +139,14 @@ module ActiveRecord # Computes the table name, (re)sets it internally, and returns it. def reset_table_name #:nodoc: - if superclass.abstract_class? + if abstract_class? + self.table_name = if superclass == Base || superclass.abstract_class? + nil + else + superclass.table_name + end + elsif superclass.abstract_class? self.table_name = superclass.table_name || compute_table_name - elsif abstract_class? - self.table_name = superclass == Base ? nil : superclass.table_name else self.table_name = compute_table_name end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index a109c50e2a..1cc815794f 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -27,6 +27,13 @@ require 'rexml/document' require 'active_support/core_ext/exception' require 'bcrypt' +class FirstAbstractClass < ActiveRecord::Base + self.abstract_class = true +end +class SecondAbstractClass < FirstAbstractClass + self.abstract_class = true +end +class Photo < SecondAbstractClass; end class Category < ActiveRecord::Base; end class Categorization < ActiveRecord::Base; end class Smarts < ActiveRecord::Base; end @@ -2088,4 +2095,8 @@ class BasicsTest < ActiveRecord::TestCase Bird.stubs(:scoped).returns(mock(:uniq => scope)) assert_equal scope, Bird.uniq end + + def test_table_name_with_2_abstract_subclasses + assert_equal "photos", Photo.table_name + end end |