Requiring files that subclass AR models

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.

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs