Rails: Tableless model that calls other models

2020-05-08 07:34发布

I have a Rails app with a few model classes (e.g. Category, Subcategory, User, etc.). In order to implement a not-too-trivial filter functionality, I built a hierarchy of filter classes: FilterCategory, FilterSubcategory, etc., that derive from FilterBase. Each of them uses the appropriate "sister" model class (e.g. Category.find :all).

I quickly realized that I can't simply call the "sister" model class without using "require" first. However, I now suspect that using "require" is the main reason for two other problems I posted here and here, which probably mess up the class caching when config.cache_classes=false.

Is there another way for me to call these other models without requiring them?

I tried using the BaseWithoutTable plugin, but when I call the "sister model", I end up getting "Not a valid constant descriptor: nil", which occurs since Rails looks for "FilterCategory::Category" rather than "Category".

Any thoughts of the best way to do that?

I'm using Rails 2.3.8, Ruby 1.8.7.

Thanks, Amit

1条回答
来,给爷笑一个
2楼-- · 2020-05-08 08:15

I wonder if you want ::Category - getting Category from the top-level namespace rather than scoping it to FilterCategory?

If your models are in the app/models directory, you shouldn't need to explicitly require them - Rails already takes care of that.

查看更多
登录 后发表回答