NameError:未初始化的常量Person :: RAILS_ROOT

时间:2015-03-05 06:07:48

标签: ruby-on-rails ruby-on-rails-4

在' lib / tasks / admin.rake':

require 'rake'
namespace :admin do
  desc "TODO"
  task fake_people: :environment do
    print "How many fake people do you want?"
    Person.all.each do |p|
      print p.first_name + ' ' + p.last_name + '********'
    end
  end
end

Person.rb(model)

require 'rake'

class Person < ActiveRecord::Base
  def admin
    load File.join(RAILS_ROOT, 'lib', 'tasks', 'admin.rake')
    Rake::Task["admin:fake_people"].invoke
  end 
end

如何在模特中从admin:fake_people致电call_rake。请帮助我,我已经失去了一整天。

1 个答案:

答案 0 :(得分:3)

尝试:

Rails.root

因为你可能有FoR 4.RARS ROOT在RoR 2或3中工作正常。