rails rake 任务是否提供对 ActiveRecord 模型的访问? [英] Do rails rake tasks provide access to ActiveRecord models?
本文介绍了rails rake 任务是否提供对 ActiveRecord 模型的访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建自定义 rake 任务,但我似乎无权访问我的模型.我认为这是 rails 任务中隐含的内容.
I am trying to create a custom rake task, but it seems I dont have access to my models. I thought this was something implicitly included with rails task.
我在 lib/tasks/test.rake 中有以下代码:
I have the following code in lib/tasks/test.rake:
namespace :test do
task :new_task do
puts Parent.all.inspect
end
end
这是我的父模型的样子:
And here is what my parent model looks like:
class Parent < ActiveRecord::Base
has_many :children
end
这是一个非常简单的示例,但我收到以下错误:
It's a pretty simple example, but I get the following error:
/> rake test:new_task
(in /Users/arash/Documents/dev/soft_deletes)
rake aborted!
uninitialized constant Parent
(See full trace by running task with --trace)
有什么想法吗?谢谢
推荐答案
想通了,任务应该是这样的:
Figured it out, the task should look like:
namespace :test do
task :new_task => :environment do
puts Parent.all.inspect
end
end
注意 =>:environment
依赖添加到任务中
Notice the => :environment
dependency added to the task
这篇关于rails rake 任务是否提供对 ActiveRecord 模型的访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文