Ruby-on-rails rails rake 任务是否提供对 ActiveRecord 模型的访问?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/876396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:15:06  来源:igfitidea点击:

Do rails rake tasks provide access to ActiveRecord models?

ruby-on-railsactiverecordraketask

提问by gmoniey

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.

我正在尝试创建自定义 rake 任务,但似乎我无权访问我的模型。我认为这是 rails 任务中隐含的内容。

I have the following code in lib/tasks/test.rake:

我在 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)

Any ideas? Thanks

有任何想法吗?谢谢

回答by gmoniey

Figured it out, the task should look like:

想通了,任务应该是这样的:

namespace :test do
  task :new_task => :environment do
    puts Parent.all.inspect
  end
end

Notice the => :environmentdependency added to the task

注意=> :environment添加到任务中的 依赖项

回答by Luke Schafer

you might need to require your configuration (which should specify all your required models etc)

你可能需要你的配置(它应该指定你所有需要的模型等)

eg:

例如:

require 'config/environment'

alternatively you can just require each seperately, but you might have environment issues AR not set up etc)

或者,您可以单独要求每个,但您可能会遇到环境问题 AR 未设置等)

回答by ocodo

When you begin writing your raketasks, use a generator to stub them out for you.

当您开始编写rake任务时,请使用生成器为您存根。

For example:

例如:

rails g task my_tasks task_one task_two task_three 

You'll get a stub created in lib/tasks called my_tasks.rake(obviously use your own namespace.) Which will look like this:

您将在 lib/tasks 中创建一个名为my_tasks.rake(显然使用您自己的命名空间)的存根。它看起来像这样:

namespace :my_tasks do

  desc "TODO"
  task :task_one => :environment do 
  end  

  desc "TODO"
  task :task_two => :environment do 
  end  

  desc "TODO"
  task :task_three => :environment do 
  end  

end

All your rails models etc. will be available for the current environment from within each task block, unless you're using the productionenvironment, in which case you need to require the specific models you want to use. Do this within the body of the task. (IIRC this varies between different versions of Rails.)

您的所有 rails 模型等都将可用于每个任务块内的当前环境,除非您使用的是生产环境,在这种情况下,您需要要求使用您想要使用的特定模型。在任务正文中执行此操作。(IIRC 这在不同版本的 Rails 之间有所不同。)

回答by apadana

With the new ruby hash syntax (Ruby 1.9) the environment will be added like this to the rake task:

使用新的 ruby​​ 哈希语法(Ruby 1.9),环境将像这样添加到 rake 任务中:

namespace :test do
  task new_task: :environment do
    puts Parent.all.inspect
  end
end

回答by Lex Lindsey

The :environment dependency is quite correctly called out, but rake still may not know about other gems that your models depend on - in one case of mine, 'protected_attributes'.

:environment 依赖项被正确地指出,但 rake 可能仍然不知道您的模型所依赖的其他 gem - 在我的一种情况下,'protected_attributes'。

The answer is to run:

答案是运行:

bundle exec rake test:new_task

This guarantees that the environment includes any gems specified in your Gemfile.

这保证了环境包含您的 Gemfile 中指定的任何 gem。

回答by Ni3

Generate task using below command (namespace with task name):

使用以下命令生成任务(带有任务名称的命名空间):

rails g task test new_task

Use below syntax to add logic:

使用以下语法添加逻辑:

namespace :test do
  desc 'Test new task'
  task new_task: :environment do
    puts Parent.all.inspect
  end
end

Run above task using below command:

使用以下命令运行上述任务:

bundle exec rake test:new_task  

or

或者

 rake test:new_task