Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

do not compute table names for abstract classes #2679

Merged
merged 1 commit into from

3 participants

@amatsuda
Collaborator

I found a trivial inconsistency on AR table_name computing behavior.

While there are an abstract_class model and its child:

class User < ActiveRecord::Base
  self.abstract_class = true
end

class Admin < User
end

The child's table_name changes depending on whether the abstract model's table_name were called or not.

User.table_name
=> "users"
User.scoped.to_sql
=> SELECT "users".* FROM "users"
Admin.table_name
=> "users"
Admin.scoped.to_sql
=> SELECT "users".* FROM "users"

Admin.table_name
=> "admins"
Admin.scoped.to_sql
=> SELECT "admins".* FROM "admins"

This doesn't matter for normal use, but sometimes causes weird errors, for example, when dealing with an AR extension plugin that scans through descendants of AR::Base on Railtie hook.

Here's a patch that makes abstract models always skip compute_table_name.

@tenderlove tenderlove merged commit 5345958 into from
@amatsuda
Collaborator

Thank you @tenderlove!
Can you please backport this to 3-1-stable?

@tenderlove
Owner

@amatsuda done!

@amatsuda
Collaborator

@tenderlove Thank you!

@route

What about scopes?

class User < ActiveRecord::Base
  self.abstract_class = true
  scope :something, where(:name => 'something')
end

class Admin < User
end

Admin.something.to_sql => "SELECT \"admins\".* FROM \"admins\"  WHERE \"\".\"name\" = 'something'"

And we have missed table name in where statment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 24, 2011
  1. @amatsuda
This page is out of date. Refresh to see the latest.
View
2  activerecord/lib/active_record/base.rb
@@ -624,6 +624,8 @@ def quoted_table_name
# Computes the table name, (re)sets it internally, and returns it.
def reset_table_name #:nodoc:
+ return if abstract_class?
+
self.table_name = compute_table_name
end
View
4 activerecord/test/cases/base_test.rb
@@ -1625,6 +1625,10 @@ def test_abstract_class
assert !LooseDescendant.abstract_class?
end
+ def test_abstract_class_table_name
+ assert_nil AbstractCompany.table_name
+ end
+
def test_base_class
assert_equal LoosePerson, LoosePerson.base_class
assert_equal LooseDescendant, LooseDescendant.base_class
Something went wrong with that request. Please try again.