将一个模型中的named_scopes与另一个模型一起使用
有没有办法在另一个模型中重用一个模型中定义的命名范围。
我定义了一些通用的named_scopes来进行一些过滤,我想在另一个模型中使用它们。将它们复制到新模型似乎有点不干。
PS:我使用的是Rails 2.3.8
Is there any way to reuse named scopes defined in a model, in another model.
I have some generic named_scopes defined that do some filtering, and I would like to use them in another model.Copying them to the new model seems a bit to non-dry.
PS: I'm using Rails 2.3.8
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,只需将它们写入到 lib 目录中的文件中,并将它们包含在每个模型中即可。这样,命名范围将包含在您包含它们的每个模型中。
Sure, just write them into a file that you put in the lib directory and include them in each of the models. That way the named scopes will be included in each of the models you include them within.