ruby on rails - rake task with assignment ||= coming back nil -



ruby on rails - rake task with assignment ||= coming back nil -

why in rake task hash still returning nil?

namespace :pull_and_update_liverail_daily desc 'pull , update' task :by_date, [:date_start, :date_end] => :environment |t,args| args[:date_start] ||= datetime.yesterday.strftime('%y-%m-%d') args[:date_end] ||= datetime.yesterday.strftime('%y-%m-%d') rake::task["stats:import_all"].invoke(args[:date_start], args[:date_end]) rake::task["update_daily_stats:by_date"].invoke(args[:date_start]) end end

this works though..

date_start = args[:date_start] || datetime.yesterday.strftime('%y-%m-%d') date_end = args[:date_end] || datetime.yesterday.strftime('%y-%m-%d')

you should utilize with_defaults:

namespace :pull_and_update_liverail_daily desc 'pull , update' task :by_date, [:date_start, :date_end] => :environment |t,args| args.with_defaults(date_start: datetime.yesterday.strftime('%y-%m-%d'), date_end: datetime.yesterday.strftime('%y-%m-%d')) rake::task["stats:import_all"].invoke(args[:date_start], args[:date_end]) rake::task["update_daily_stats:by_date"].invoke(args[:date_start]) end end

when seek this:

args[:date_start] = datetime.yesterday.strftime('%y-%m-%d')

what happens is:

args.[]=(:date_start, datetime.yesterday.strftime('%y-%m-%d'))

(it calls method []=)

since taskarguments not define []= method method_missing called, tries find value of argument named :[]=:

def method_missing(sym, *args) lookup(sym.to_sym) end

which explains why values not changed, don't exception...

ruby-on-rails ruby ruby-on-rails-4 rake

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -