这似乎是个愚蠢的问题,但我有一个名为 Ad 的模型,我有一个名为 auto 的库,其中包含一个类 Ad(lib/auto.rb)。
#auto.rb lib file
module auto
class Ad
def initialize
...
end
def convert
# here I would like to access my model class Ad
# ::Ad does not work. Requiring it does not work either.
end
end
end
Rails 3 是否在某些全局 namespace 下存储模型?
最佳答案
我是不是遗漏了什么,或者您正在定义 auto::Ad?
如果是这样,那么 ::Ad
将永远无法工作。使用 auto::Ad
或 Ad
(来自 auto
模块)。
如果你真的不想要 auto
命名空间。删除代码中的 module auto
部分。
关于ruby - 从相同方法的 lib 文件中访问 Rails 模型名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5635443/