我可以按如下方式传递参数:
desc "Testing args" task: :hello, :user, :message do |t, args| args.with_defaults(:message => "Thanks for logging on") puts "Hello #{args[:user]}. #{:message}" end
我还能够为 Rails 应用程序加载当前环境
desc "Testing environment" task: :hello => :environment do puts "Hello #{User.first.name}." end
我想做的是能够拥有变量和环境
desc "Testing environment and variables" task: :hello => :environment, :message do |t, args| args.with_defaults(:message => "Thanks for logging on") puts "Hello #{User.first.name}. #{:message}" end
但这不是有效的任务调用。有谁知道我怎么能做到这一点?
task :t, [args] => [deps]
当您将参数传递给 rake 任务时,您可以使用 :needs 选项来要求环境。例如:
desc "Testing environment and variables" task :hello, :message, :needs => :environment do |t, args| args.with_defaults(:message => "Thanks for logging on") puts "Hello #{User.first.name}. #{args.message}" end
根据@Peiniau 下面的评论更新
至于 Rails > 3.1
task :t, arg, :needs => [deps] # deprecated
请用