# Constant caching, modules and STI

**URL:** <https://www.ruby-forum.com/t/constant-caching-modules-and-sti/161113>\
**Category:** Rails\
**Created:** [February 23, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/constant-caching-modules-and-sti/161113 "2009-02-23T16:52:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [February 23, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/constant-caching-modules-and-sti/161113/1 "2009-02-23T16:52:36Z")

</div>

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):

#######  
module ConstantCache  
module ClassMethods  
def caches\_constants  
const\_set(self.name.to\_s.underscore.upcase, [‘my’, ‘array’,  
‘here’])  
end  
end  
end

ActiveRecord::Base.send(:extend, ConstantCache::ClassMethods)  
########

What happens thus far is that all of the subclasses have a constant  
defined with the name of the parent class.

e.g.  
RnData::RN\_DATA  
=\> ‘12345’  
RnAuto::RN\_DATA  
=\>‘12345’  
RnAuto::RN\_AUTO

LoadError: Expected /Users/dorian/Sites/mysite/app/models/rn/  
rn\_auto.rb to define RN\_AUTO  
from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/  
active\_support/dependencies.rb:428:in `load_missing_constant' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/ active_support/dependencies.rb:77:in`const\_missing\_not\_from\_s3\_library’  
from /opt/local/lib/ruby/gems/1.8/gems/aws-s3-0.5.1/lib/aws/s3/  
extensions.rb:174:in `const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/ active_support/dependencies.rb:89:in`const\_missing’  
from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/  
active\_support/dependencies.rb:439:in `load_missing_constant' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/ active_support/dependencies.rb:93:in`const\_missing’  
from (irb):2

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?

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

any help much appreciated!  
thanks  
dorian

---

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [February 23, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/constant-caching-modules-and-sti/161113/2 "2009-02-23T17:12:43Z")

</div>

On 23 Feb 2009, at 15:51, tachekent wrote:

> 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.

Fred

---

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [February 23, 2009, 6:23pm UTC](https://www.ruby-forum.com/t/constant-caching-modules-and-sti/161113/3 "2009-02-23T18:23:32Z")

</div>

> 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.

Yeah but I have at least 17 subclasses that are basically empty stubs  
for looking up the data and setting relationships on a model.

---

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [February 23, 2009, 7:17pm UTC](https://www.ruby-forum.com/t/constant-caching-modules-and-sti/161113/4 "2009-02-23T19:17:09Z")

</div>

oh, and thanks for the ‘super’ I always wondered what that did 🙂
