table_name
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (7)
- 2.0.3 (10)
- 2.1.0 (1)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-1)
- 3.1.0 (0)
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
table_name()
public
Guesses the table name (in forced lower-case) based on the name of the class in the inheritance hierarchy descending directly from ActiveRecord::Base. So if the hierarchy looks like: Reply < Message < ActiveRecord::Base, then Message is used to guess the table name 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. You can add new inflections in config/initializers/inflections.rb.
Nested classes are given table names prefixed by the singular form of the parent’s table name. Enclosing modules are not considered.
Examples
class Invoice < ActiveRecord::Base; end; file class table_name invoice.rb Invoice invoices class Invoice < ActiveRecord::Base; class Lineitem < ActiveRecord::Base; end; end; file class table_name invoice.rb Invoice::Lineitem invoice_lineitems module Invoice; class Lineitem < ActiveRecord::Base; end; end; file class table_name invoice/lineitem.rb Invoice::Lineitem lineitems
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" end
Examples of Setting the Prefix and Suffix
Both of the following will work for setting the prefix or suffix:
class Mouse < ActiveRecord::Base self.table_name_prefix = 'forum_' end
and
class Mouse < ActiveRecord::Base def self.table_name_prefix 'forum_' end end