在Rails 3.2中更改为Rake任务和参数结构?

n8g*_*ard 3 rake ruby-on-rails

我看过其他帖子但我仍然遇到麻烦.以下是我的代码.我有几个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
Run Code Online (Sandbox Code Playgroud)

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

$ rake my_namespace:show_user_accounts customer_id=110
Run Code Online (Sandbox Code Playgroud)

错误:

rake aborted!
Couldn't find Customer with id=0
Run Code Online (Sandbox Code Playgroud)

n8g*_*ard 5

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

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

$ rake my_namespace:show_user_accounts[110]
Run Code Online (Sandbox Code Playgroud)

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