问题
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