I have my Rails 5 ApplicationRecord
:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
and a bunch of models that inherit from ApplicationRecord
. Now, I would like each model name to map to its database table in a customized way. For example, I can do:
class MyModel < ApplicationRecord
self.table_name = 'MY_MODELS' # overrides default 'my_models'
end
But since all the mappings are predictable, I thought I would just do that in the base class:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def self.table_name
self.name.underscore.pluralize.upcase
end
end
...but that didn't work, even though technically class methods should be inherited by the subclass. (Any idea why?)
I also tried adding a method that did something like this:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def self.inherited(subclass)
subclass.table_name = subclass.name.underscore.pluralize.upcase
end
end
This didn't work either. Any idea how I can do this?