aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-08-16 09:46:43 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-08-16 09:46:43 +0000
commit14101c7b40c2e04303cb497eb60996add2de645f (patch)
tree64bd2dcc67a78ed686e9efca6b37354f014d7c56 /activerecord/lib/active_record
parent80f1597942dbfdc24d532d9ebd555969deea12fe (diff)
downloadrails-14101c7b40c2e04303cb497eb60996add2de645f.tar.gz
rails-14101c7b40c2e04303cb497eb60996add2de645f.tar.bz2
rails-14101c7b40c2e04303cb497eb60996add2de645f.zip
Nested classes are given table names prefixed by the singular form of the parent's table name.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4770 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/base.rb26
1 files changed, 20 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 4a0ef4c203..d3a8abd016 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -588,21 +588,35 @@ module ActiveRecord #:nodoc:
# to guess the table name from even when called on Reply. The rules used to do the guess are handled by the Inflector class
# in Active Support, which knows almost all common English inflections (report a bug if your inflection isn't covered).
#
- # Additionally, the class-level table_name_prefix is prepended to the table_name and the table_name_suffix is appended.
- # So if you have "myapp_" as a prefix, the table name guess for an Account class becomes "myapp_accounts".
+ # Nested classes are given table names prefixed by the singular form of
+ # the parent's table name. Example:
+ # file class table_name
+ # invoice.rb Invoice invoices
+ # invoice/lineitem.rb Invoice::Lineitem invoice_lineitems
#
- # You can also overwrite this class method to allow for unguessable links, such as a Mouse class with a link to a
- # "mice" table. Example:
+ # Additionally, the class-level table_name_prefix is prepended and the
+ # table_name_suffix is appended. So if you have "myapp_" as a prefix,
+ # the table name guess for an Invoice class becomes "myapp_invoices".
+ # Invoice::Lineitem becomes "myapp_invoice_lineitems".
+ #
+ # You can also overwrite this class method to allow for unguessable
+ # links, such as a Mouse class with a link to a "mice" table. Example:
#
# class Mouse < ActiveRecord::Base
- # set_table_name "mice"
+ # set_table_name "mice"
# end
def table_name
reset_table_name
end
def reset_table_name #:nodoc:
- name = "#{table_name_prefix}#{undecorated_table_name(base_class.name)}#{table_name_suffix}"
+ # If this is a nested class, prefix with singular parent table name.
+ if parent < ActiveRecord::Base && !parent.abstract_class?
+ contained = parent.table_name
+ contained = contained.singularize if parent.pluralize_table_names
+ contained << '_'
+ end
+ name = "#{table_name_prefix}#{contained}#{undecorated_table_name(base_class.name)}#{table_name_suffix}"
set_table_name(name)
name
end