Hi there,
I've been following one of the recipes in the Advanced Rails Recipes
books, and have arrived for my own particular needs at this scenario:
I want to cache some options data in constants at loadtime (as per the
recipe) for dropdown menus in a form
All of the different options are in a single table and use STI.
I have the following module in my initializer folder, with
caches_constant called on the main activerecord data class (RnData):
Ideally I would like something along the lines of:
RnData::RN_DATA
RnData::RN_AUTO
RnData::RN_INCOME
or just
RnAuto::OPTIONS
RnIncome::OPTIONS
as long as it has the right info for the subclass in it, and not just
the parent inherited across all subclass.
So, I'm wondering if there's a way to write this so that it will run
caches_constant for subclass, or do I have to include the
caches_constants line in all my subclasses?
The inherited hook as you found below would do the trick. feels a bit
overkill to me - sometimes there is such a thing as too DRY.
I've replacing caches_constants with this which seems to work, but I'm
worried I may be overwriting something critical in activerecord:
class RnData < ActiveRecord::Base
def self.inherited(subclass)
subclass.caches_constants
end
end
at the very least call super here or you will be overwriting stuff here.