- 此问题在此处已有答案**:
Do rails rake tasks provide access to ActiveRecord models?(6个答案)
昨天关门了。
我正在尝试运行一个rake任务添加到我的数据库。但是我得到下面的错误。如果任何人有这个错误或知道解决方案,任何帮助将不胜感激!
rails aborted!
NameError: uninitialized constant User
/Users/richardbatt/Desktop/Ruby/NelsonParksProject/lib/tasks/users.rake:31:in `block (3 levels) in <main>'
/Users/richardbatt/Desktop/Ruby/NelsonParksProject/lib/tasks/users.rake:22:in `each'
/Users/richardbatt/Desktop/Ruby/NelsonParksProject/lib/tasks/users.rake:22:in `block (2 levels) in <main>'
/Users/richardbatt/Desktop/Ruby/NelsonParksProject/bin/rails:4:in `<main>'
Tasks: TOP => users:xero_users
(See full trace by running task with --trace)
namespace :users do
desc "Gets Users from Xero"
task :xero_users do
require 'xero-ruby'
creds = {
client_id: '...',
client_secret: '...',
grant_type: 'client_credentials'
}
xero_client = XeroRuby::ApiClient.new(credentials: creds)
@token_set = xero_client.get_client_credentials_token
# save @token_set
accessToken = @token_set["access_token"]
users = xero_client.accounting_api.get_users('').users
users.each do |user|
userID = user.user_id
userEmailAddress = user.email_address
userFirstName = user.first_name
userLastName = user.last_name
userIsSubscriber = user.is_subscriber
userOrganisationRole = user.organisation_role
User.create(email: userEmailAddress)
end
end
end
我试图将新用户添加到我的数据库中。当我使用"User. create(email:"测试")"在我的家庭控制器中,它添加到数据库中。只是不是在我运行rake任务的时候。
谢谢你!
1条答案
按热度按时间62lalag41#
Rake任务不会自动加载Ruby on Rails环境。
当您希望在Rake任务中使用Rails模型时,必须通过更改以下行命令Ruby加载Rails环境
到