【问题标题】:Change to Rake tasks and argument structure in Rails 3.2?在 Rails 3.2 中更改为 Rake 任务和参数结构?
【发布时间】:2012-03-28 13:58:38
【问题描述】:

我看过其他帖子,但我仍然遇到问题。下面是我的代码。我有几个 rake 任务,我传入零个、一个甚至五个参数。我错过了什么?

namespace :my_namespace do
  desc 'shows user accounts within the database for the specified customer.'
  task :show_user_accounts, [:customer_id] => :environment do |t, args|

    cust = Customer.find( args.customer_id.to_i )
    cust.users.each do |user|
      puts "User Name: #{user.name}\tUser ID: #{user.id}\t"
    end
  end
end

我正在使用以下命令运行任务:

$ rake my_namespace:show_user_accounts customer_id=110

错误:

rake aborted!
Couldn't find Customer with id=0

【问题讨论】:

标签: ruby-on-rails rake


【解决方案1】:

经过大量搜索,我发现不仅 rake 任务的语法发生了变化,而且执行语法也发生了变化。所以,我的 rake 任务(上图)的代码是正确的,但我的调用是错误的。

上面运行rake任务的正确方法是:

$ rake my_namespace:show_user_accounts[110]

我在这里找到了答案:http://www.redconfetti.com/2012/01/example-rake-task/

【讨论】:

  • 链接的文章id错误,在链接的网站上找不到!
  • 当我这样做时,我收到一条消息说找不到任务[whatever],而没有参数的任务工作正常......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 2016-04-08
  • 1970-01-01
相关资源
最近更新 更多