在 Squeel 中引用命名范围的语法是什么?

发布于 2024-12-12 10:57:56 字数 661 浏览 1 评论 0原文

有没有办法使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧梦荧光笔 2024-12-19 10:57:56

Squeel 目前不支持引用命名范围。首选方法是使用 Squeel 筛子,然后在您的范围中使用筛子:

sifter :continuous { where{ job_type_id == 1 }}
sifter :standard   { where{ job_type_id == 2 }}

scope :continuous, -> { where{ sift(:continuous) }}
scope :standard,   -> { where{ sift(:standard) }}
scope :active,     -> { where{ (job_status_id == 2) & (sift(:continuous) | (sift(:standard) & (date_start > Time.now) & (date_end < Time.now)) }}

显然仍然有一些重复,并且可能不是最好的示例或用途,但只是想展示如何使用它们来实现您的示例。

参考筛选器: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:

sifter :continuous { where{ job_type_id == 1 }}
sifter :standard   { where{ job_type_id == 2 }}

scope :continuous, -> { where{ sift(:continuous) }}
scope :standard,   -> { where{ sift(:standard) }}
scope :active,     -> { where{ (job_status_id == 2) & (sift(:continuous) | (sift(:standard) & (date_start > Time.now) & (date_end < Time.now)) }}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文