简体   繁体   中英

Rails: Tableless model that calls other models

I have a Rails app with a few model classes (eg 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 (eg 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM