我正在为Ruby on Rails应用程序编写rake任务。我目前只是确保它正在访问正确的字段,并且到目前为止已经写了这个:
namespace :abc do
desc "Used to generate a new daily log"
task :create_post => :environment do
User.find_each do |currentUser|
STARTING_DATE = currentUser.start_date
puts STARTING_DATE
end
puts "It worked yo"
end
end
当我运行此rake任务时,输出到命令行是:
$ rake abc:create_post
2017-05-02 03:34:20 UTC
C:/Users/vanbeeandr/workspace/online_journal/lib/tasks/create_post.rake:7: warning: already initialized constant STARTING_DATE
C:/Users/vanbeeandr/workspace/online_journal/lib/tasks/create_post.rake:7: warning: previous definition of STARTING_DATE was here
2017-04-02 03:34:28 UTC
It worked yo
我希望输出只是:
$ rake abc:create_post
2017-05-02 03:34:20 UTC
2017-04-02 03:34:28 UTC
It worked yo
我是否错误地指定了STARTING_DATE
变量?我并不习惯Ruby,所以我不确定为什么会导致错误。
感谢任何帮助!
答案 0 :(得分:1)
案例在Ruby中很重要,UPPERCASE_NAMES
表示常量。您不应该重新分配它们,当您重复为循环内的常量赋值(即使是相同的值)时,您正在执行此操作。
或者:
starting_date = currentUser.start_date
)