# Requiring files that subclass AR models

**URL:** <https://www.ruby-forum.com/t/requiring-files-that-subclass-ar-models/132627>\
**Category:** Rails\
**Created:** [March 22, 2008, 2:30am UTC](https://www.ruby-forum.com/t/requiring-files-that-subclass-ar-models/132627 "2008-03-22T02:30:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [March 22, 2008, 2:30am UTC](https://www.ruby-forum.com/t/requiring-files-that-subclass-ar-models/132627/1 "2008-03-22T02:30:08Z")

</div>

What was the intended way to include files that extend AR models?

For example, I have a class called

UserAction \< ActiveRecord::Base  
end

I would like to include test.rb file in environment.rb that subclasses  
UserAction like so :

CommentUserAction \< UserAction  
end

However, if I do this with cache\_classes = false, then things will get  
screwed up because UserAction will get unloaded between requests.

I tried to use “require\_dependency ‘test.rb’”, but I was running into  
problems with that as well.

In the past we’d use something like “include Reloadable” but thats  
deprecated now.

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [March 22, 2008, 7:35am UTC](https://www.ruby-forum.com/t/requiring-files-that-subclass-ar-models/132627/2 "2008-03-22T07:35:59Z")

</div>

Aryk G. wrote:

> CommentUserAction \< UserAction
> 
> Any help would be greatly appreciated.

Can you put test.rb in the lib or models directories as  
comment\_user\_action.rb?

If not, below your “require test.rb” in environment.rb  
add “CommentUserAction”. This should trigger loading of  
both classes.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [March 22, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/requiring-files-that-subclass-ar-models/132627/3 "2008-03-22T21:22:43Z")

</div>

I can’t put comment\_user\_action in lib or models directories because in  
my case I have like 15 of these classes that extend UserAction, and  
making 15 files is a pain in the butt.

As for putting “CommentUserAction” under the require line in the  
environment.rb. Im not sure how helpful this would be because, the code  
from environment.rb only gets run once. So I think I’d still run into  
the same problem once UserAction gets unloaded between requests.

I solved it by putting require\_dependency “test.rb” in application.rb.  
This works since application.rb gets reloaded between requests, but I’d  
hate to be adding model requires in the application.rb file.

Any thoughts?

Aryk

Mark Reginald J. wrote:

> Aryk G. wrote:
> 
> > CommentUserAction \< UserAction
> > 
> > Any help would be greatly appreciated.
> 
> Can you put test.rb in the lib or models directories as  
> comment\_user\_action.rb?
> 
> If not, below your “require test.rb” in environment.rb  
> add “CommentUserAction”. This should trigger loading of  
> both classes.
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.
