Rails 3使用嵌套模块中的模型进行自动加载

时间:2014-07-01 18:18:07

标签: ruby-on-rails ruby ruby-on-rails-3

我们正在对Rails应用程序进行主要数据库更改。为了能够与现有代码互操作,我的计划是在模块命名空间中完成所有工作,以使它们与现有模型分开。但是,我遇到了Rails自动加载问题。

我的文件结构如下:

app/
  models/
    entity/
      new_thing.rb
    old_thing.rb

其中new_think.rb包含类似

的内容
module Entity
  class NewThing
  end
end

和old_thing.rb包含类似

的内容
class OldThing
end

OldThing自动加载,但我一直收到这样的错误:

Expected app/models/entity/new_thing.rb to define NewThing

有没有办法可以让它正确地期望entity / new_thing.rb定义Entity::NewThing

1 个答案:

答案 0 :(得分:0)

尝试:

在你的old_thing.rb

class OldThing
  Extend Entity
end

class OldThing
  require "entity/new_thing"
end