Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 0d1df723c79300b73691d7785ee87d7b74caf591 1 parent ac153fe
@drogus drogus authored
View
10 activerecord/lib/active_record/model_schema.rb
@@ -139,10 +139,14 @@ def quoted_table_name
# 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
View
11 activerecord/test/cases/base_test.rb
@@ -27,6 +27,13 @@
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 @@ def test_uniq_delegates_to_scoped
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
Please sign in to comment.
Something went wrong with that request. Please try again.