How to call an array of scopes on an ActiveRecord object?

[亡魂溺海] 提交于 2019-12-11 07:39:40

问题


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]

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

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


回答1:


just use inject:

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


来源:https://stackoverflow.com/questions/27082253/how-to-call-an-array-of-scopes-on-an-activerecord-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!