【问题标题】:Access named scope dynamically动态访问命名范围
【发布时间】:2013-02-21 11:16:10
【问题描述】:

如果我有 3 个命名范围,例如

class Foo

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

还有一个函数

def x(variable)

end

其中变量是字符串(“test1”、“test2”或“test3”)

如何仅通过知道该变量的值来访问命名范围?

类似Foo.variable

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rails-activerecord named-scope named-scopes


    【解决方案1】:

    你会打电话给Foo.public_send(variable)

    【讨论】:

    • 就是这样 :D 。谢谢m8!
    • 如果它解决了您的问题,请随时接受答案:)
    • 在 7 分钟内:D。限制的东西:))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    相关资源
    最近更新 更多