Rails 从 2.2.2 移动到 2.3.8 - rake 未加载与控制台或我的应用程序相同的环境
我有一个已经存在了一段时间的应用程序,我正在迁移它 轨道 2.3.8。有很多有趣的问题,但我正在 冲刺,但现在遇到了一些非常奇怪的错误。
要点是当我用 rake 运行任务时它会失败,但是当我运行 控制台中的相同代码可以正常工作。我设计的“修复”是 将我的 include ModuleNameA、include ModuleNameB 等移动到底部 AR 文件。
我不喜欢这个“修复”,因为我不明白它。在我移动它们之前 AR 对象(我们称之为 Bob)不断抛出未定义的方法 通过 rake 调用调用时出错。它被调用了 在 rake 任务中,但在另一个 AR 对象中。
所以 rake 任务正在运行类似 Worker.work 的东西,它会 如上所述失败。当我从控制台运行 Worker.work 时 会过去的。一旦我将包含内容移至文件底部, 会起作用的。
有人见过这样的事情吗?我感觉像什么 基本面不正确。就像我破坏了一些基本的东西 功能和我的“修复”是某种奇怪的补丁。
谢谢。 Erik
PS:
我在 AR 对象中包含了一个模块。它通过以下方式添加类和实例方法:
def self.included(base)
base.extend(ClassMethods)
end
该模块中的所有方法都可以在控制台中使用,但不能通过 rake 任务使用。
更新: 我注意到如果我采取 :work =>; :环境部分超出 rake 任务,而不是在任务中执行块需要环境 手动文件(例如 require(File.join(RAILS_ROOT, 'config', “环境”)))它工作正常。突然间全班同学 方法是可用的。这非常令人不安。
I have an app that has been around a while that I'm migrating for
Rails 2.3.8. There have been a lot of interesting issues, but I'm on
the home stretch, but have now run into some very odd errors.
The gist is when I run a task with rake it fails, but when I run the
same code form the console it works fine. The 'fix' I devised was to
move my include ModuleNameA, include ModuleNameB, etc to the bottom of
the AR file.
I don't like this 'fix' as I don't understand it. Before I moved them
the AR object (we will call it Bob) kept throwing undefined method
errors when it was called through the rake invocation. It was invoked
in the rake task, but in another AR object.
So the rake task was running something like Worker.work, and it would
fail as described above. When I run Worker.work from the console it
would pass. Once I moved the includes to the bottom of the file both
would work.
Anyone ever seen anything like this? I feel like something
fundamental isn't correct. Like I have broken something basic to the
functionality and my 'fix' is some kind of weird patch.
thanks.
Erik
PS:
There is a module that I'm including in my AR object. It adds class and instance methods by doing:
def self.included(base)
base.extend(ClassMethods)
end
All of the methods in this module are available in the console, but not through rake tasks.
Update:
I noticed that if i take the :work => :environment part out of the
rake task and instead in the task do block require the environment
file by hand (e.g. require(File.join(RAILS_ROOT, 'config',
'environment'))) it works fine. All of a sudden all of my class
methods are available. this is very disturbing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我想通了......
我们在 app/models 下有一个名为 Deals 的目录,该目录未包含在加载路径中。这就是我的模块给我带来所有问题的地方。为什么它可以从控制台加载,而不是通过 rake 加载......我不知道。
一旦我将其添加到 config.load_path 中,一切就开始正常工作。我从来没有注意到这一点,因为在 2.2.2 中一切都工作正常,应用程序处于生产或开发模式,控制台和 rake 任务处于开发、测试和生产模式。
我认为这只是我做错的基本事情,因为错误很迟钝,而且我的“修复”看起来非常糟糕。
So I figured it out....
We had a directory under app/models called deals that was not included in the load path. That is where my module was that was giving me all the issues. Why it was being loaded OK from the console and not with rake...I don't know.
Once I added this to the config.load_path everything has started working fine. I never noticed this because in 2.2.2 everything worked fine, the app in production or dev mode, the console and the rake tasks in dev, test, and production mode.
I figured it was something just basic i was doing wrong as the error was obtuse and my 'fixes' looked like really bad ideas.