Access named scope dynamically

ε祈祈猫儿з 提交于 2019-12-13 03:06:27

问题


If I have 3 named scopes like

class Foo

scope :test1, ...
scope :test2, ...
scope :test3, ...

And a function

def x(variable)

end

where variable is a string("test1","test2" or "test3")

How can I access the named scope just by knowing that variable's value ?

Something like Foo.variable


回答1:


You would call Foo.public_send(variable).



来源:https://stackoverflow.com/questions/15000854/access-named-scope-dynamically

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