在 Squeel 中引用命名范围的语法是什么?
有没有办法使用 Squeel 来引用已经存在的范围?
考虑以下情况:
scope :continuous, where{ job_type_id == 1 }
scope :standard, where{ job_type_id == 2 }
scope :active, where{ (job_status_id == 2) & ((job_type_id == 1) | ((job_type_id == 2) & (date_start > Time.now) & (date_end < Time.now))) }
所有三个范围都正常工作,但前两个范围(连续
和标准
)的逻辑在第三个范围内重复,这就是我想要的通过执行以下操作来避免:
scope :active, where{ (job_status_id == 2) & (continuous | (standard & (date_start > Time.now) & (date_end < Time.now))) }
...除了我在 Squeel DSL 中找不到引用命名范围的正确语法之外。
有没有办法做我想做的事,或者我只需要明确说明?
Is there a way, using Squeel, to refer to already existing scopes?
Consider the following:
scope :continuous, where{ job_type_id == 1 }
scope :standard, where{ job_type_id == 2 }
scope :active, where{ (job_status_id == 2) & ((job_type_id == 1) | ((job_type_id == 2) & (date_start > Time.now) & (date_end < Time.now))) }
All three scopes work properly, but the logic from the first two (continuous
and standard
) are duplicated within the third, which is what I'd like to avoid, by doing something like:
scope :active, where{ (job_status_id == 2) & (continuous | (standard & (date_start > Time.now) & (date_end < Time.now))) }
... except that I can't find the correct syntax in the Squeel DSL for referring to named scopes.
Is there a way to do what I'd like, or do I just need to be explicit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Squeel 目前不支持引用命名范围。首选方法是使用 Squeel 筛子,然后在您的范围中使用筛子:
显然仍然有一些重复,并且可能不是最好的示例或用途,但只是想展示如何使用它们来实现您的示例。
参考筛选器:https://github.com/ernie/squeel#sifters
Squeel currently doesn't support referencing named scopes. The preferred method is using Squeel sifters and then using the sifters in your scopes:
Clearly still some repetition, and probably not the best example or use, but just wanted to show how to implement your example with them.
Reference sifters: https://github.com/ernie/squeel#sifters