简体   繁体   中英

NameError: uninitialized constant Person::RAILS_ROOT

In '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

In 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

How do i call admin:fake_people from call_rake in model.Please help me i have lost a whole day.

Try:

Rails.root

Because probably you have RoR 4. RAILS_ROOT was work fine in RoR 2 or 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM