如何在 ActiveRecord 对象上调用范围数组? [英] How to call an array of scopes on an ActiveRecord object?

查看:34
本文介绍了如何在 ActiveRecord 对象上调用范围数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Article 模型,它具有 recentcompiledfeatured 范围.我可以像这样链接这些范围:

I have an Article model which has recent, compiled, featured scopes. I can chain these scopes like this:

Article.recent.compiled.featured

现在我有这些范围的数组:

Now I have an array of these scopes:

scopes = [:recent, :compiled, :featured]
or
scopes = [:recent, :compiled]

我事先不知道 scopes 变量中有多少个作用域.我只知道这是一个范围数组.

I don't know beforehand how many scopes are there in the scopes variable. I just know it's an array of scopes.

如何使用此数组作为要在上述 Article 模型上调用的范围链?

How can I use this array as a chain of scopes to be called on the Article model as mentioned above?

推荐答案

只需使用注入:

scopes.inject(Article){ |ar,scope| ar.send(scope) }

这篇关于如何在 ActiveRecord 对象上调用范围数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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