当我的系统需要两个同名的类或模块时,我该怎么做才能明确我的意思?
我正在使用 Rails(新的),我的一个模型被命名为“Thread”。当我尝试引用 thread_controller.rb 中的类“Thread”时,系统返回一些其他同名常量。
<thread.rb>
class Thread < ActiveRecord::Base
def self.some_class_method
end
end
<thread_controller.rb>
class ThreadController < ApplicationController
def index
require '../models/thread.rb'
@threads = Thread.find :all
end
end
当我尝试 Thread.find() 时,我收到一条错误消息,提示 Thread 没有名为 find 的方法。当我访问 Thread.methods 时,我没有在其中找到我的 some_class_method 方法。
有什么帮助吗? (并且不要费心发布“只需将您的模型命名为其他名称即可。”指出明显的妥协没有帮助。)
最佳答案
您可以将您的应用放入其自己的命名空间中。
<my_app/thread.rb>
module MyApp
class Thread
end
end
关于ruby-on-rails - ruby : importing two modules/classes of the same name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4190518/