Rails:调用其他模型的无表模型 [英] Rails: Tableless model that calls other models

查看:42
本文介绍了Rails:调用其他模型的无表模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有几个模型类(例如 CategorySubcategoryUser 等)的 Rails 应用.为了实现一个非常重要的过滤器功能,我构建了一个过滤器类的层次结构:FilterCategoryFilterSubcategory 等,它们派生自 FilterBase.他们每个人都使用适当的姐妹"模型类(例如 Category.find :all).

我很快意识到我不能在不首先使用require"的情况下简单地调用sister"模型类.但是,我现在怀疑使用require"是我发布的另外两个问题的主要原因 此处此处,当 config.cache_classes=false 时,这可能会弄乱类缓存.

还有其他方法可以让我在不需要它们的情况下调用这些其他模型吗?

我尝试使用 BaseWithoutTable 插件,但是当我调用姐妹模型"时,我最终得到不是有效的常量描述符:nil",这是因为 Rails 寻找FilterCategory::Category"而不是Category".

有没有想到最好的方法来做到这一点?

我使用的是 Rails 2.3.8、Ruby 1.8.7.

谢谢,阿米特

解决方案

我想知道您是否想要 ::Category - 从顶级命名空间获取 Category 而不是将其范围限定为 FilterCategory?

如果您的模型位于 app/models 目录中,则您不需要明确要求它们 - Rails 已经处理了这一点.

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

解决方案

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.

这篇关于Rails:调用其他模型的无表模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆