Posted about 2 years ago. Visible to the public. Repeats.

Rails: namespacing models with table_name_prefix instead of table_name

When you want to group rails models of a logical context, namespaces are your friend. However, if you have a lot of classes in the same namespace it might be tedious to specify the table name for each class seperately:

class Accounting::Invoice < ApplicationRecord self.table_name = 'accounting_invoices' ... end class Accounting::Payment < ApplicationRecord self.table_name = 'accounting_payments' ... end

A replacement for the self.table_name-assignment is the table_name_prefix in the module definition:

module Accounting def self.table_name_prefix 'accounting_' end end class Accounting::Invoice < ApplicationRecord ... end class Accounting::Payment < ApplicationRecord ... end

Rails will be able to derive the table name accounting_invoices for Accounting::Invoice.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Daniel Straßner
Last edit:
about 2 years ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Daniel Straßner to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more